允许用户在swift 2.0中播放背景音乐



我正在寻找一些代码,允许用户从他们的手机播放音乐,同时仍然使用我的应用程序。以前在swift 2.0之前,我会把它放在应用程序委托,它会完美地工作:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)

有人知道如何在swift 2.0中实现这个吗?

以下是Swift 2在AVSession上调用setCategorysetActive的语法:

do
{
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    try AVAudioSession.sharedInstance().setActive(true)
}
catch let error as NSError
{
    print(error)
}

do
{
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    try AVAudioSession.sharedInstance().setActive(true)
}
catch let error as NSError
{
    print(error)
}

最新更新