Search code examples
iosiphoneavaudiosessionheadsetspeaker

Play two song on two iphone output [iOS]


Here the problem, I want to play a song using the default iphone speaker and an other using the headset in the same time, is it possible ?

I use two AVAudioSession in my case.

I trie using this but this property is apply to all the AVaudioSession :

 [mySession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];

Thanks


Solution

  • It is not possible to output different audio streams to the speaker and the headset concurrently.

    As of iOS 6, it is possible to output different audio streams to separate outputs using the AVAudioSessionCategoryMultiRoute category. This is set using:

    // Retrieve session instance
    AVAudioSession *session = [ AVAudioSession sharedInstance ];
    // Register for Route Change notifications
    [[NSNotificationCenter defaultCenter] addObserver: myObject
         selector: @selector(handleRouteChange:)
         name: AVAudioSessionRouteChangeNotification
         object: session];
    // Request the MultiRoute category
    [ session setCategory:AVAudioSessionCategoryMultiRoute error:&errRet ];
    // Set our session to be active
    [ session setActive:YES error:&errRet ];
    

    Full details can be found in the WWDC 2012 Session 505 video on AudioSession and MultiRoute Audio. Be sure to watch the full video and not just the read PDF since it contains a full demo of the capabilities.

    It isn't obvious from the session exactly how to route different audio to different outputs in code, so finding a solution may require some trial and error, or a question on the CoreAudio-API mailing list.


    This doesn't solve your problem but you can also force the audio to output through the speakers when the headphones are plugged in. See UI Hacker - iOS: Force audio output to speakers while headphones are plugged in.