AirPlay - programatically disconnect before switching the other device - ios

I have an application for video streaming. It has ability to make mirroring AirPlay (Apple Tv). Now I'm trying to add Google Chromecast connection. I added button and everything working just I expected. Here is my case: I want user only be able to connect 1 device at the moment. For example if user already conneceted to AirPlay when user tap on Chromecast, I want to disable AirPlay programmatically.
Here's how I checked external playback status:
self.videoPlayer.isExternalPlaybackActive
Here's how I checked is AirPlay connected:
- (BOOL)isAirPlayConnected {
return self.airPlayButton.isWirelessRouteActive;
}
I know this is how I disabled all external display:
self.videoPlayer.allowsExternalPlayback = NO;
self.videoPlayer.usesExternalPlaybackWhileExternalScreenIsActive = NO;
I already listening:
MPVolumeViewWirelessRoutesAvailableDidChangeNotification and MPVolumeViewWirelessRouteActiveDidChangeNotification notifications.
Bu couldn't find solution for programatically disconnect before switching the other.
Any help would be appreciated.

Related

How to handle SharePlay initiated via AirPlay Button

We recently implemented SharePlay in our broadcasting app and most of things work fine.
We now found out, that the user, during a SharePlay session, is able to hand over the session to an Apple TV via the AirPlay Button, which is convenient UX, because during the SharePlay session, this should be exactly what the user had in mind.
However, since this is not the start of a "real" or "normal" AirPlay, things are different: Especially AVPlayers isExternalPlaybackActive property doesn't change in case it's a SharePlay handover. This leads to problems in our app with AirPlay and SharePlay behaviour. Most of all, the user cannot control the playback (pause, seek) anymore from his device, but only from her/his Apple TV, only other participants of the SharePlay session can.
How can we detect the handover to Apple TV?
Is there an alternative to isExternalPlaybackActive for the SharePlay case?

MPVolumeview volumeseider disappears when Airplay connected

I have a video application in which I am using Airplay to stream the video. I am using the below code.
let airView: MPVolumeView = MPVolumeView()
airView.showsRouteButton = true
airView.showsVolumeSlider = true
airView.sizeToFit()
airView.tintColor = UIColor.black
airView.autoresizingMask = UIViewAutoresizing.flexibleWidth
airplayButton = UIBarButtonItem(customView: airView)
airplayButton.tintColor = UIColor.white
self.navigationItem.setRightBarButtonItems([airplayButton], animated: true)
Using the code below, I can see the Airplay picker button, I can also see the volume slider however when I am connecting the airplay, the volume button disappears so our clients can not make volume up and down using the phone hardware button.
Is there a way to have either to prevent the disappearance of that button or support hardware volume support while playing Airplay?
Additional info:
When using older AirPlay devices such as an old AppleTV that users AirPlay1 the volume controls should work. However, when the same app is connected to an AirPlay2 device, the volume controls are removed. And in fact the data screen for the MPVolumeSlider is removed. The slider will return when connected back to the Phone or Bluetooth speakers.
Recently Apple deprecated the Router Button on MPVolumeView starting with iOS 13 and this new router view does not include a volume slider. It appears that Apple wants us to use either the rocker, airplay mirroring or the volume controls that the AVRouterView holds. This change becomes very confusing to the user as sometimes the slider is there and sometimes it isn't. Even if the slider is shown disabled with another slider, the user does not understand why they can use the rocker and sliders in the AVRouterView and not on the app itself with AirPlay2. They also don't even understand what AirPlay1 vs AirPlay2. Things such as metadata even work differently.

Adding airplay support to MPMoviePlayer

Hi i am building iOS application target iOS 5 and above. I am using MPMoviePlayerController to stream movie file. I that player i want to give airplay support.
According to apple documentation
Default Movie players (iOS 4.3 and later) support wireless movie
playback to AirPlay-enabled hardware such as Apple TV. The movie
player presents a control that allows the user to choose
AirPlay-enabled hardware for playback when such hardware is in range.
It seems that by default it will display airplay button in fullscreen mode if any airplay device is in range. But it is not seems to be happening. I am unable to see airplay button in movie player.
Am i missing something? Please advise.
By default, MPMoviePlayerController enables AirPlay support. If you haven't set it as FALSE. Then, probably you need to check your network or AppleTV's network connection. They need to be in same network (intranet usually).
There is a safe way to detect whether your iOS device is able to find AppleTV. You can double click HOME button, swipe task bar from left to right side, if you can see "Airplay" route button, then you can reach it. Otherwise, you need to check your AppleTV setting to make sure Airplay function is enabled and AppleTV is connected to same network that you iOS device in.
If you are using your own customized video play controller interface, you can embedded the "Airplay" button into your own UI.
MPVolumeView

How to display overlay on Apple TV via AirPlay

I am developing an iOS app that displays a video, e.g., a football game, on Apple TV via AirPlay. I want to display additional information, e.g., player stats, on the big screen while the video is playing.
I am aware of the Redfin approach, where they require the user to turn on AirPlay mirroring first. Unfortunately, this is not acceptable for us. We want it to be obvious to users on how to show the video.
We are currently presenting an AirPlay Route button before displaying the video to allow the user to set it up using the following code.
self.airPlayPicker = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
self.airPlayPicker.showsVolumeSlider = NO;
self.airPlayPicker.showsRouteButton = YES;
[self.view addSubview:self.airPlayPicker];
The Route button will show when there is an Apple TV around, allowing the user to turn it on. We then present the video with MPMoviePlayerController.
When AirPlay is turned on and the video is playing, in code, I see only one UIScreen, but two UIWindows. But both UIWindows have the same dimensions as the iPhone. When I add a subview to either UIWindow, the subview always shows up on the iPhone.
Has anyone figured out how to present an overlay on top of the video on Apple TV? How do I even find the view object where the video is hosted?
I am aware that MPMoviePlayerController is built on top of AVPlayer. Would using AVPlayer give us better control of the UI?
As far as I know, this shouldn't be possible. When using AirPlay without mirroring, only the URL of the video is sent to the Apple TV. It is then up to the Apple TV to actually play the media.
Mirroring is the way to do it.

Clearing after-image from Airplay device, using UIWebView

I send a secondary UI to AirPlay - on iOS 5.1.
On the iPad, the user may use a UIWebView to play a video, and if they have Airplay setup, the video will be sent to the AirPlay device. When the video finishes playing or if the user pauses, the UIWebView does not clear out the image from the AirPlay screen.
If I then load a different URL to the UIWebView, the AirPlay image will clear out, but only after a few seconds.
I've tried to work-around this by loading a blank page, and then reloading the original video page, but that does not seem to work. Any thoughts?
In my effort to also clear other types of content from a UIWebView, I happened to discover that the following statement will also disconnect the AirPlay video connection immediately, and thereby revealing any underlying secondary UI sent via UIScreen.
[myWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML = \"\";"];

Resources