Reputation: 11
I'm trying to cast music on TV through airplay using MPMusicPlayerController. How can I check if the airplay is being used by other inputs?
Upvotes: 1
Views: 2046
Reputation: 11492
I had this problem as well, and I ended up using arp -n to get the mac address of the airplay device and then used tcpdump to sniff traffic to it:
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.
The full script ended up being a bit involved, so I wrote it up in a blog post.
Upvotes: 0
Reputation: 1468
Here is another solution to detect whether airplay is active
- (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];
}
Upvotes: 0
Reputation: 227
Couple of existing posts which may be of use:
Is there any notification for detecting AirPlay in Objective-C?
How to customize the Airplay button when airplay is active
There is a post in the second link that defines the method - (BOOL)isAirPlayActive
which neatly uses the AudioSession framework to determine the current audio output route.
Upvotes: 1