我正在寻找一些代码,允许用户在使用我的应用程序的同时从手机播放音乐.以前在
swift 2.0之前,我会把它放在应用程序委托中,它会很好的工作:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient,error: nil) AVAudioSession.sharedInstance().setActive(true,error: nil)
有谁知道如何在swift 2.0中实现?
解决方法
以下将是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)
}