2

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

Le Brun
  • 87
  • 7
  • I think its not possible. Because by using `AVAudioSession`, you can only override the players running in the iphone, you can hear the two different songs at same time but cannot hear a different songs in different outputs like speaker and headset. – Balaji Kondalrayal Aug 07 '14 at 09:52

1 Answers1

3

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.

j b
  • 5,147
  • 5
  • 41
  • 60
  • The WWDC 2012 Session 505 code sample is a bit deceptive. Before assigning channels to the AVAudioPlayer it is important to get the value of its numberOfChannels property. The number of channels you assign to the player must be exactly equal to this number. If not, the channelAssignments are nil. From the AVAudioPlayer documentation for the channelAssignments property: "The default value for this property is nil. When non-nil, this array must have the same number of elements as returned by the numberOfChannels property" – Paul Linsay Jan 13 '16 at 19:59