如何检查播放是否正在使用?

时间:2013-06-04 07:05:35

标签: audio airplay mpmusicplayercontroller

我正试图通过使用MPMusicPlayerController的airplay在电视上播放音乐。如何检查其他输入是否正在使用airplay?

3 个答案:

答案 0 :(得分:1)

可能有用的几个现有帖子:

Is there any notification for detecting AirPlay in Objective-C?

How to customize the Airplay button when airplay is active

第二个链接中有一个帖子定义方法- (BOOL)isAirPlayActive,它巧妙地使用AudioSession框架来确定当前的音频输出路径。

答案 1 :(得分:0)

以下是另一种检测播放是否有效的解决方案

- (BOOL)isAirPlayActive
{
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    AVAudioSessionRouteDescription* currentRoute =  audioSession.currentRoute;
    AVAudioSessionPortDescription* output = [currentRoute.outputs firstObject];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    return [output.portType isEqualToString:AVAudioSessionPortAirPlay];
}

答案 2 :(得分:0)

我也遇到了这个问题,最后我使用arp -n来获取airplay设备的mac地址,然后使用tcpdump来嗅到它的流量:

ipaddress=$(ping -c 1 $tvhostname | awk -F'[()]' '/PING/{print $2}')
arp -n $ipaddress &> /var/tmp/arp-output
fieldindex='$4'
# Parse something of the form ? (10.37.109.150) at 40:33:1a:3d:e6:ee on en0 ifscope [ethernet] 
# The awk quotes get a bit messy with the variable substitution, so split the expression up
echo Parsing mac address from line `awk -F"[ ]" "/\($ipaddress\)/{print}" /var/tmp/arp-output`
macaddress=`awk -F"[ ]" "/($ipaddress)/{print $fieldindex}" /var/tmp/arp-output`

sudo tcpdump -i $wifidevice -I ether dst $macaddress &> /var/tmp/airplay-tcpdump-output
# Get the PID of the tcpdump command
pid=$!
# Capture 10 seconds of output, then kill the job
sleep 10
sudo kill $pid
# Process the output file to see how many packets are reported captured
packetcount=`awk -F'[ ]' '/captured/{print $1}' /var/tmp/airplay-tcpdump-output`
echo Finished sniffing packets - there were $packetcount. 

完整的剧本最终有点参与,所以我用blog post写了一遍。