I need to list the audio outputs available to an iOS application. My question is similar to this one: How to list available audio output route on iOS
i tried this code:
NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback
error: &setCategoryError];
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
…
NSLog(@"session.currentRoute.outputs count %d", [[[[AVAudioSession sharedInstance] currentRoute] outputs ] count]);
for (AVAudioSessionPortDescription *portDesc in [[[AVAudioSession sharedInstance] currentRoute] outputs ]) {
NSLog(@"-----");
NSLog(@"portDesc UID %@", portDesc.UID);
NSLog(@"portDesc portName %@", portDesc.portName);
NSLog(@"portDesc portType %@", portDesc.portType);
NSLog(@"portDesc channels %@", portDesc.channels);
}
However I always see just one output port (the count is 1), also if I have two (an Airplay and a Built-in speaker). If I use the Music application I am able to see both ports and switch between them. In my app I only see the one that I have selected.
There is something else that I need to do?
Thank you
EDIT:
i tried this code, too:
CFDictionaryRef asCFType = nil;
UInt32 dataSize = sizeof(asCFType);
AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &asCFType);
NSDictionary *audioRoutesDesc = (__bridge NSDictionary *)asCFType;
NSLog(@"audioRoutesDesc %@", audioRoutesDesc);
but the dictionary list just one output destinations. Moreover the input sources array is empty (I have a iPhone 4s)
EDIT2:
I got something working using MPVolumeView . This component has a button that lets you choose the output audio route, like in the Music App.
If you want you can hide the slider (and have only the button) using:
self.myMPVolumeView.showsVolumeSlider = NO;
Try something like this, its more than you need but you can pare it down:
It will depend on your AVAudioSession category.
You can safely assume on an iPhone that you have at least a microphone as input and a speaker as output. If you're trying to get a list of Bluetooth/AirPlay outputs, first you'd have to make sure your session category is reporting them to you:
Then a non-intuitive way to get available outputs is to check
AVAudioSession.availableInputs
as usually a bluetooth HFP device would have a mic too.. I might be assuming a lot right now.. but that's the only way to consistently get your availableOutputs.A better way is to use MultipleRoute category which will give you more freedom at accessing
AVAudioSessionPort