Swift: how to set up an audio session that gracefully mixes with others (aka Pod)

吃可爱长大的小学妹 提交于 2019-12-24 02:37:04

问题


This code snippet makes the other audio (aka iPod) to stop:

func setSessionPlayer() {

    var audioSessionError: NSError?
    let audioSession = AVAudioSession.sharedInstance()

    audioSession.setActive(true, error: nil)

    if audioSession.setCategory(AVAudioSessionCategoryPlayback, withOptions:AVAudioSessionCategoryOptions.MixWithOthers,
        error: &audioSessionError) {
            println("Successfully set the audio session")
    } else {
        println("Could not set the audio session")
    }

}

What am I missing?


回答1:


I think it's because you're setting the audioSession.active before it's configured to MixWithOthers. Move audioSession.setActive below the if block like so:

if audioSession.setCategory(AVAudioSessionCategoryPlayback, withOptions:AVAudioSessionCategoryOptions.MixWithOthers,
    error: &audioSessionError) {
        println("Successfully set the audio session")
} else {
    println("Could not set the audio session")
}

audioSession.setActive(true, error: nil)


来源:https://stackoverflow.com/questions/26655402/swift-how-to-set-up-an-audio-session-that-gracefully-mixes-with-others-aka-pod

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