'How do I stop AirPlay audio with AVPlayer?
I'm working on an iOS app that, for reasons we won't get into here, wants to sometimes disable AirPlay playback on a remote device, and only play video locally on the iOS device, regardless of the user's settings.
I'm using AVPlayer to play the video, and that's currently non-negotiable.
I'm setting the iOS 5.0+ AVPlayer property allowsAirPlayVideo to NO, which prevents the video from playing remotely. But if the user has turned on AirPlay on the iOS device, the audio is still streamed through the remote device.
I don't see a comparable allowsAirPlayAudio flag in Apple's documentation, nor have I found mention of this issue anywhere else.
For example, this Stack Overflow question:
Audio Output Routes for AirPlay
talks about "audio output destinations in a USB audio accessory", which doesn't sound like what I need.
I don't want to just turn off audio, I want the audio to keep being played through my iOS device.
Am I missing something? Thanks!
Solution 1:[1]
Possible a duplicate of How to detect if a bluetooth headset plugged or not IOS 8?
I know it's an old question, but hey, it may help some one
Solution 2:[2]
This is what I do to disconnect the audio, mConnectDisconnect is boolean parameter.
allowsExternalPlayback will remove the image from airplay and play it on your device screen.
AVAudioSessionCategoryOptionDefaultToSpeaker this option will let you change audio to the device
AVAudioSession* session = [AVAudioSession sharedInstance];
self.player.allowsExternalPlayback = mConnectDisconnect;
if(allowsExternalPlayback){
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowAirPlay error:nil];
}else{
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
}
[session setActive:true error:nil];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | César Bermúdez |
