即使在Swift中使用新的ViewController,你如何让音乐继续前进?

时间:2014-12-14 13:22:03

标签: swift

我想在UIViewController中播放一个mp3文件。但是,当用户离开视图控制器时,音乐停止。我该如何继续比赛?

var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("musicFileHere", ofType: "mp3")!)!
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()    

1 个答案:

答案 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