在 swift 2.0 中允许用户使用背景音乐
Allow users background music in swift 2.0
我正在寻找一些代码,以允许用户在仍在使用我的应用程序的同时播放他们 phone 中的音乐。以前在 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)
}
我正在寻找一些代码,以允许用户在仍在使用我的应用程序的同时播放他们 phone 中的音乐。以前在 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)
}