MPMoviePlayerController replay video - ios

I want to display a button that gives the option to the user to restart a video in a MPMoviePlayerController at any time, even while the video is playing. I tried this but it has no effect:
-(IBAction)ReloadVideo:(id)sender{
[moviePlayer play];
}
Why doesn't this work?

You should call first [moviePlayer stop] this will:
stops playback of the current item and resets the playhead to the
start of the item. Calling the play method again initiates playback
from the beginning of the item.
(From apple docs)

Related

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.

iOS: play audio file at a specific time and stop after a specific duration

I have an audio file with length of 500 miniseconds for my app. I want app users to play the audio at minisecond 100 (by pressing a button) and the audio will automatically stops at minisecond 150. This is a code that I have done so far:
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:filePath]
error:nil] autorelease];
[audioPlayer setCurrentTime:100.00f];
[audioPlayer play];
Would you please help me with the stopping part? Thank you
You could create an instance of NSTimer with the desired play interval, then when the timer fires, stop audio. Create the timer on the main thread, and in a place in your code in an applicable area where you can get a reference for the audioPlayer. Make sure you handle interruptions, IE, User pauses or stops audio somehow, or if the audioPlayer is deallocated somewhere, your app goes into the background, or whatever else you need to handle, by invalidating and disposing of the timer. See the docs for information on NSTimer. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html

AVAudioPlayer Loses "Playing" Status

I have an AVAudioPlayer that needs to continue in the background.
Audio is set as the background mode in the plist & this runs on launch:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[RootController shared].view becomeFirstResponder];
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setDelegate: self];
[session setActive:YES error:nil];
[session setCategory: AVAudioSessionCategoryPlayback error:nil];
- (BOOL)canBecomeFirstResponder { return YES; }
The Problem
Occasionally the AVAudioPlayer gets in this strange state where:
It's playing, but the play icon in the status bar disappears
If I pause then play, the icon shows up for maybe a second, then disappears
Here's the kicker - if I call setCurrentTime while playing, the play icon shows & stays
I've sunk about 20 hours into this & would love any ideas.
Description of the Bug
If you are playing an AVAudioPlayer then you create an AVPlayer, the playing icon will disappear. Apparently AVPlayer immediately takes precedence & since it is not playing yet the play icon disappears from the status bar.
This causes some serious issues:
If the app is in the background, iOS will shut down your AVAudioPlayer within 5 seconds (because it doesn't realize you're playing audio)
The iOS remote shows a play button even though audio is playing
The play icon is not showing in the status bar
The Workaround
First off, if you don't have to use AVPlayer, then don't. I use it because I need to play a remote MP3 without downloading it first. I used to use this AudioStreamer class but gave up because it pops up an alert when the stream becomes disconnected along with a few other bugs that I couldn't fix.
So if you're stuck with AVPlayer, there's only one way to re-connect playing status with your AVAudioPlayer. If you call setCurrentTime on the AVAudioPlayer then it will magically re-associate itself as the current player for the app. So you'll need to call it after any AVPlayer is initialized and anytime you resume playback on your AVAudioPlayer.
I decided to subclass AVAudioPlayer so I could register it in a global list (when it is initialized) and unregister it when it is deallocated. I also overrode the play method so that any calls to resume playback would also call setCurrentTime. Then I subclassed AVPlayer so that any time one is initialized, all active AVAudioPlayers call setCurrentTime on themselves. Last thing - you'll have to call setCurrentTime after a short, maybe 1 second, delay or else it will have no effect.
No kidding, this is the result of nearly 40 hours of troubleshooting.

Now playing "play/pause" button image

In the multimedia controls (in the multitasking UI) the "play" button always show the pause image even when the music is paused, everything works fine except this
this problem don't happen if the background audio comes from the Music App (i.e. the button image switch from play to pause and vice-versa as expected)
how to fix?
thanks
In my case, I had to pause all the AV-related items which were "playing":
[myAVAudioPlayer pause];
[myAVAudioRecorder pause];
[[MPMusicPlayerController applicationMusicPlayer] pause];
After which, the play/pause button on the lock screen switches, and the playhead also freezes. Inverse for play.
Change MPNowPlayingInfo dictionary, set new parameter
MPNowPlayingInfoPropertyPlaybackRate to 1.0f to show pause button
MPNowPlayingInfoPropertyPlaybackRate to 0.0f to show play button
see my answer how to use this code.

Tell video loaded in MPMoviePlayerViewController to stop playing

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!

Resources