環境
- OSX Yosemite v10.10.4
- Xcode v7.0 beta6
- 検証端末 iPhone5s/iPhone6 v8.4.1
AVAudioPlayerUtil.swift
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する場所の記述
AVAudioPlayerUtil.setValue(
NSURL(
fileURLWithPath: NSBundle.mainBundle().pathForResource(
"/*音声ファイル名*/",
ofType: "/*音声ファイル拡張子*/")!
)
);
AVAudioPlayerUtil.play();