我正在寻找一些代码,允许用户在使用我的应用程序的同时从他们的手机播放音乐。在 Swift 2.0 之前,我可以将此代码放到应用代理中,它可以完美地工作:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
有人知道如何在Swift 2.0中实现这个吗?
我正在寻找一些代码,允许用户在使用我的应用程序的同时从他们的手机播放音乐。在 Swift 2.0 之前,我可以将此代码放到应用代理中,它可以完美地工作:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
以下是在Swift 2中调用AVSession
的setCategory
和setActive
的语法:
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)
}