Tell video loaded in MPMoviePlayerViewController to stop playing - ipad

I've got a navigation view with a table view in it, listing some videos. When a row is selected, it loads MPMoviePlayerViewController and inits it with a video from file URL. When I go back to the table view, the movie is still playing. I tried getting the underlying MPMoviePlayerController and giving it a "pause" message in the viewDidDisappear method, but this doesn't seem to ever get called (NSLog statement in method never appears). So I'm sure there's a simple way to tell MPMoviePlayerController via MPMoviePlayerViewController to stop playing it's movie programmatically, right?

Simply needed to subclass MPMoviePlayerViewController, load the subclass from the table/navigation on selection, then add this to that subclass:
-(void)viewWillDisappear:(BOOL)animated {
[self.moviePlayer stop];
}

You need to register for some notifications. See the 'Notifications' section of the MPMoviePlayerController class reference:
http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html
In particular, register to receive MPMoviePlayerDidExitFullscreenNotification and MPMoviePlayerPlaybackDidFinishNotification and in your method that is called when these notifications are sent, stop the movie from playing by sending the 'stop' message.

But what about when you want to stop downloading the video before it is finished playing. For example, when you go to a different screen.
For me, i try to stop the video when the videoWillDisappear method gets called. Yet, the video still downloads even when the current video is gone!

Related

tvOS issue with Siri search deeplinking integration

I have an issue where when user is playing a video , while video is being player, he uses siri to search for a different movie, which will load its corresponding movie details page and then select to play that movie, which deeplinks to your app which is playing a movie, when i play a new selected movie and dismiss avplayer and avplayercontroller, audio from previous video still continues to play. somehow avplayer is not cleared although i clear all subviews from window and initialize its super view controller class again. I am cluless what can i do erase older instance of avplayer. Let me know if anyone has any suggestions or faced similar issue.
A few suggestions:
Are you subclassing AVPlayerViewController? If so, that's a bad idea. The API docs specifically say not to do that.
Add a deinit function. If it's not being called when the old AVPlayer is dismissed, you know you have a retention problem. This is often caused by registering for notifications or boundary time observers.
If you view controller has a reference to the AVPlayer object, you might try overriding the viewDidDisappear function to call player.pause() and then setting the player reference first to a new instance of AVPlayer() then to nil. Not sure why this helps, but sometimes it does.
Definitely implement #2 above. If deinit is not getting called, you most certainly have an issue.

iOS: AVPlayer play won't play sometimes

So I have an app built with a player that plays a video, I have a [player pause] and [player play] in the didBecomeActive and willResignActive methods. Most of the time works fine, but when I open the app, and press the home button and repeat again that process, around the 8th time the video will not play even though I see the play method getting called.
Does anyone have any idea what might be going on?
The app can be in several states that are not foreground. Before playing, check to see if you still have a player, that it still has a player.currentItem, and if it's status is AVPlayerStatusReadyToPlay.
If any of those conditions are not met, then the player and the item must be reinitialized using the code that you used to create it in the first place.
This is a good candidate for a lazy initializer for your player property.

How to control background music

I am developing an app with a background music and the music starts when the first view (INTROViewController) appears (it is implemented in viewDidLoad). I have created a settings page (OPTIONSViewController) where I have implemented a switch to turn the music on and off. In OPTIONSViewController I have created an instance of INTROViewController where all the audio files and the commands to play and stop the music are defined. I don't get any errors but it seems the instance is not able to control the music at all, which continues playing. What am i doing wrong? Cheers
I think u can have just one AVPlayer on your AppDelegate.
Declare it like a property, and then u can get this player from all your application
with something like this:
yourAppDelegate* appDele= (yourAppDelegate*)[[UIApplication sharedApplication]delegate];
AVPlayer * tempPlayer = [appDele yourPlayer];
This should fix your problem :D

Playing sections of a movie

Is it possible to play sections of a movie without using timers to do the pausing and unpausing? For example I have a movie loaded into MPMoviePlayerController, the user taps and the first section of the movie is played then the movie is paused, on the second tap another section is played and so on. Setting the initialPlayBackTime and endPlayBackTime of MPMoviePlayerController doesn't help as endPlayBackTime gets updated only when stop is called (could it be a bug?), but stopping causes an annoying flicker.
I don't think so... You need the timer and that's it.

Using multiple MPMoviePlayerController instances in a UITabBarController based iOS app

In my iPad app, the user can open one MPMoviePlayerController in each tab (total of 5).
In each viewcontroller containing a movieplayer instance, I play the movie in viewDidAppear: method, and I pause it in viewWillDisappear: method. I also add/remove the viewcontroller as an observer for notifications so that only the viewcontroller currently visible is receiving the notifications. The movieplayer is setup in the init method, and there is nothing really special about it.
Note: Although you can create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time can play its movie.
Besides the note above, I cannot find anything about this in the docs, but I´m guessing that the movieplayer is shared between viewcontrollers somehow, becasuse when I force one movieplayer in each tab it works sometimes, and sometimes the movieplayer is finished when I return to the previous tab.
Is it a fact that MPMoviePlayerController are meant only handle one video at a time, no matter if the instances are in different classes/tabs?

Resources