swift - 即使在 Swift 中使用新的 ViewController,如何让音乐继续播放?

标签 swift

我想在 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/

相关文章:

ios - 实现Apple TV collectionview水平无限滚动

ios - AVPlayer 的定时元数据

ios - 可选类型 CGFloat 的值?没有拆封,你是想用吗!或者?

swift - 为什么这个 Swift 代码不进行类型检查?

ios - 我如何支持 iOS7 上的自调整单元格?

ios - 如何使用 Swift 根据 UITextView 文本长度启用和禁用 Alertaction 按钮

swift - OpenSSL - 主题在 OS X 应用程序中不以 '/' 开头

php - 如何将变量从 Swift 传递到 PHP 以用于 mySQL 查询?

ios - 将自定义 iOS 框架导入两个不同的 Xcode 项目不会得到相同的结果

ios - 快速将 UILabel 添加到 UITextView