環境
- OSX Yosemite v10.10.4
- Xcode v7.0 beta6
- 検証端末 iPhone5s/iPhone6 v8.4.1
AVAudioPlayerUtil.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import AVFoundation struct AVAudioPlayerUtil { static var audioPlayer:AVAudioPlayer = AVAudioPlayer(); static var sound_data:NSURL = NSURL(); static func setValue(nsurl:NSURL){ self.sound_data = nsurl; self.audioPlayer = try! AVAudioPlayer(contentsOfURL: self.sound_data); self.audioPlayer.prepareToPlay(); } static func play(){ self.audioPlayer.play(); } } |
playする場所の記述
1 2 3 4 5 6 7 8 |
AVAudioPlayerUtil.setValue( NSURL( fileURLWithPath: NSBundle.mainBundle().pathForResource( "/*音声ファイル名*/", ofType: "/*音声ファイル拡張子*/")! ) ); AVAudioPlayerUtil.play(); |