5

我已经查看但找不到访问音频输出路由的方法,因此我可以检测音频是否通过 AirPlay 输出。

这是我在 iOS 5.0 文档中找到的

kAudioSessionOutputRoute_AirPlay

讨论

这些字符串用作与 kAudioSession_AudioRouteKey_Outputs 数组关联的字典的 kAudioSession_AudioRouteKey_Type 键的值。

我找不到访问 kAudioSession_AudioRouteKey_Outputs 数组的方法。

谢谢

4

3 回答 3

5

即使 Bassem 似乎找到了解决方案,为了完整起见,以下是如何检测当前输出路由是否为 AirPlay:

- (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;
}

请记住,您必须#import <AudioToolbox/AudioToolbox.h>链接到 AudioToolbox 框架。

于 2012-09-11T06:24:00.003 回答
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.

于 2015-03-21T10:18:01.603 回答
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);

我不太确定确切的语法,所以你不得不稍微弄乱它,但你应该明白一般的想法。

于 2011-12-04T22:53:04.403 回答