SRGSSR / srgmediaplayer-apple

An advanced media player library, simple and reliable
MIT License
158 stars 33 forks source link

Improve AirPlay route prioritization on iOS 13 #69

Closed defagos closed 4 years ago

defagos commented 5 years ago

Starting with iOS 13, a new prioritizesVideoDevices property has been added to AVRoutePickerView. As the name suggests, this setting prioritizes video devices in the route list.

It would be tempting to improve our current implementation as follows:

- (void)updateAppearanceForMediaPlayerController:(SRGMediaPlayerController *)mediaPlayerController
{
    UIButton *airPlayButton = nil;

    // `AVRoutePickerView` is a button with no image, with layers representing the AirPlay icon instead. If we need
    // to display an image the original icon layers needs to be hidden first.
    if (@available(iOS 11, *)) {
        BOOL hasImage = (self.image != nil);

        airPlayButton = self.routePickerView.srg_airPlayButton;
        airPlayButton.imageView.contentMode = hasImage ? UIViewContentModeCenter : UIViewContentModeScaleToFill;

        self.routePickerView.activeTintColor = self.activeTintColor;
        self.routePickerView.srg_isOriginalIconHidden = hasImage;

#ifdef __IPHONE_13_0
        if (@available(iOS 13, *)) {
            self.routePickerView.prioritizesVideoDevices = (mediaPlayerController.mediaType == SRGMediaPlayerMediaTypeVideo);
        }
#endif
    }

    // ....
}

In effect, and if no custom icon has been provided at the SRGAirPlayButton level, the default route icon is either a TV AirPlay icon (prioritizesVideoDevice set to YES) or the audio AirPlay icon (set to NO).

We could use this new property, but this probably requires several icons to be defined, as well as a consistent behavior for iOS 12 and below. More work than I naively expected, thus this proper dedicated issue.

defagos commented 4 years ago

With recent changes to reduce the need for playback observers, it is easier to implement this feature. I already have a working first implementation (which needs to be tested on older devices), but this is promising enough to think it will be part of the next release.

defagos commented 4 years ago

Behavior could be extended for iOS 12 and below (since we are already tweaking the AirPlay icon).