Allow users background music in swift 2.0

喜你入骨 提交于 2020-01-01 09:04:03

问题


I am looking for some code to allow the user to play music from their phone while still using my app. Previously before swift 2.0 i would put this in the app delegate and it would work perfectly:

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

Does anyone know how to implement this in swift 2.0?


回答1:


The following would be the syntax for Swift 2 calling setCategory and setActive on AVSession:

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

OR

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


来源:https://stackoverflow.com/questions/32673916/allow-users-background-music-in-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!