我想在 UIViewController
中播放 mp3 文件。不过,当用户离开 View Controller 时,音乐就会停止。我怎样才能让它继续播放?
var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("musicFileHere", ofType: "mp3")!)!
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
最佳答案
添加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
关于swift - 即使在 Swift 中使用新的 ViewController,如何让音乐继续播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469740/