5

I have looked but can't find a way to access the Audio Output Routes so i can detect if the audio is coming out via AirPlay.

This is what i found in the Documentation for iOS 5.0

kAudioSessionOutputRoute_AirPlay

Discussion

These strings are used as values for the kAudioSession_AudioRouteKey_Type key for the dictionary associated with the kAudioSession_AudioRouteKey_Outputs array.

I can't find a way to get access to the kAudioSession_AudioRouteKey_Outputs array.

Thanks

Bassem
  • 119
  • 2
  • 7

3 Answers3

5

Even if Bassem seems to have found a solution, for completion's sake, here's how to detect whether the current output route is AirPlay or not:

- (BOOL)isAirPlayActive{
    CFDictionaryRef currentRouteDescriptionDictionary = nil;
    UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &currentRouteDescriptionDictionary);
    if (currentRouteDescriptionDictionary) {
        CFArrayRef outputs = CFDictionaryGetValue(currentRouteDescriptionDictionary, kAudioSession_AudioRouteKey_Outputs);
        if (outputs) {
            if(CFArrayGetCount(outputs) > 0) {
                CFDictionaryRef currentOutput = CFArrayGetValueAtIndex(outputs, 0);
                CFStringRef outputType = CFDictionaryGetValue(currentOutput, kAudioSession_AudioRouteKey_Type);
                return (CFStringCompare(outputType, kAudioSessionOutputRoute_AirPlay, 0) == kCFCompareEqualTo);
            }
        }
    }

    return NO;
}

Keep in mind that you have to #import <AudioToolbox/AudioToolbox.h> and link against the AudioToolbox framework.

Ben Baron
  • 14,496
  • 12
  • 55
  • 65
avf
  • 858
  • 11
  • 24
1

Since iOS 6, the recommended approach for this would be using AVAudioSession (the C-based AudioSession API is deprecated as of iOS 7).

let currentRoute = AVAudioSession.sharedInstance().currentRoute

currentRoute returns an AVAudioSessionRouteDescription, a very simple class with two properties: inputs and outputs. Each of these is an optional array of AVAudioSessionPortDescriptions, which provides the information we need about the current route:

if let outputs = currentRoute?.outputs as? [AVAudioSessionPortDescription] {
    // Usually, there will be just one output port (or none), but let's play it safe...
    if let airplayOutputs = outputs.filter { $0.portType == AVAudioSessionPortAirPlay } where !airplayOutputs.isEmpty {
        // Connected to airplay output...
    } else {
        // Not connected to airplay output...
    }
}

The portType is the useful info here... see the AVAudioSessionPortDescription docs for the AVAudioSessionPort... constants that describe each input/output port type, such as line in/out, built in speakers, Bluetooth LE, headset mic etc.

Also, don't forget to respond appropriate to route changes by subscribing to the AVAudioSessionRouteChangeNotification.

Stuart
  • 36,683
  • 19
  • 101
  • 139
0
CFArray *destinations;
CFNumber *currentDest;

// Get the output destination list
AudioSessionGetProperty(kAudioSessionProperty_OutputDestinations, nil, destinations);

// Get the index of the current destination (in the list above)
AudioSessionGetProperty(kAudioSessionProperty_OutputDestination, nil, currentDest);

Im not too sure of the exact syntax, so you'll have to mess around with it a bit, but you should get the general idea.

craig1231
  • 3,769
  • 4
  • 31
  • 34
  • kAudioSessionProperty_OutputDestinations >A CFArrayRef object containing details on the available audio output destinations in a USB audio accessory attached through the iPad camera connection kit. Each element of the array contains a CFDictionaryRef object with the keys and corresponding values described in “USB Accessory Audio Destination Dictionary Keys.” I can't use this because its only used for USB connected devices. – Bassem Dec 05 '11 at 00:21
  • I figured out how to get the audio out put of the device but can't answer my own question just yet because i'm a noob and don't have 100+ rep. I will post the answer in 5 hours :) – Bassem Dec 05 '11 at 00:41