我想在UIViewController
中播放一个mp3文件。但是,当用户离开视图控制器时,音乐停止。我该如何继续比赛?
var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("musicFileHere", ofType: "mp3")!)!
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
答案 0 :(得分:1)
添加audioPlayer.play
,以便您的整个代码如下所示:
var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("musicFileHere", ofType: "mp3")!)!
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play