AirPlay support, MPMoviePlayerController and MPVolumeView relation

spd picture spd · Apr 20, 2011 · Viewed 7.1k times · Source

I am developing an iPhone application that has support for video play. I am using MPMoviePlayerController with custom controls for playing the video. For this purpose I have set control style of MPMoviePlayerController to MPMovieControlStyleNone.

I would like to support AirPlay feature for the video being played. As per the documentation, we have to set the 'allowsAirPlay' property of MPMoviePlayerController to YES to enable AirPlay feature. How can I display the AirPlay button on my player UI if I am using MPMoviePlayerController with custom controls?

I have tried the following:

  1. Instantiated MPVolumeView
  2. Set the showsRouteButton and showsVolumeSlider properties of MPVolumeView to NO to hide the volume slider and route button
  3. Added MPVolumeView on my custom player View

I have not given the reference of MPVolumeView and MPMoviePlayerController to each other. But, if 'allowsAirPlay' of MPMoviePlayerController is set to YES then AirPlay button gets displayed on MPVolumeView. How are MPVolumeView and MPMoviePlayerController related? Please let me know the connection between these two classes which are created independently.

Thanks and Regards, Deepa

Answer

devdavid picture devdavid · Aug 8, 2011

Since the MPMoviePlayerController only allows you to play one video at a time, the MediaPlayer framework always knows the video that's playing. That's how MPVolumeView knows about the MPMoviePlayerController. I have no official docs, but I imagine it's baked into the framework this way.

Since there are probably a lot of checks and balances going on (and they loves consistent UIs), Apple only allows you to use their AirPlay button/UI for tapping into this feature. You can, however, put that button wherever you want:

airplayButton = [[MPVolumeView alloc] init];
airplayButton.frame = CGRectMake(myX, myY, 40, 40);
[airplayButton setShowsVolumeSlider:NO];
[customPlayerControls.view addSubview:airplayButton];

I just guessed on the width,height being 40,40 and I'm sure it's not correct, but once I got the button in place it didn't matter.