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

This piece of code allows you to stop another sound (such as an iPod):

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?

+2
source share
1 answer

I think because you install audioSession.activebefore tuning in MixWithOthers. Move audioSession.setActivebelow the if block as follows:

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)
+3
source

Source: https://habr.com/ru/post/1623991/


All Articles