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



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

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

在Objective-C中是否有检测AirPlay的通知?

如何在播放激活时自定义播放按钮

在第二个链接中有一篇文章定义了方法- (BOOL)isAirPlayActive,该方法巧妙地使用AudioSession框架来确定当前音频输出路由。

这里是另一个检测播放是否处于活动状态的解决方案

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

我也有这个问题,我最终使用arp -n来获取播放设备的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. 

完整的脚本最终有点复杂,所以我把它写在一篇博客文章中。

最新更新