iPhone how to stop MPMoviePlayerController is closing at the end? - iphone

In my iPhone app, I'm using MPMoviePlayerController to play a movie. I'm hiding all the controls that are by default visible on the movie player. But I placed a "Replay" button over the player control.
At the end of movie, the player is being removed. But I want to stop at the last frame, so that when I click "Replay" button, it will start from beginning. I wrote the functionality for replay and its working good. When ever the movie is playing and on click of "Replay", its restarting the video from starting.
The problem I'm facing is that, at the end of movie its becoming white screen and pressing "Replay" button is not restarting the movie. How to handle this situation?

Its not necessary to create new instance. I found out the solution. Don't just release the instance of player. When replay button is clicked (either in middle of video or after completion), just pause it, move the location to beginning and play it. That's it.. no retain, no release nothing.... Its upto the programmer/developer when to release the player and remove it from the view.

I never used MPMoviePlayerController but pherhaps it's instance is released when the movie has ended. In this case incrementing the retaincounter and manually releasing it would solve the problem.

Related

How to tell the MPNowPlayingInfoCenter wether or not the music is playing or paused?

I can't seem to make iOS show the correct play / pause button in the remote audio controls. I do receive the remote control events and set all values of the nowPlayingInfo dictionary.
Everything works fine and I even see a cover photo on the lock screen. Except the pause/play button. It always looks like pause even if my AVAudioPlayer is playing. It sends a pause event regardless of playback state.
How can I notify iOS that AVAudioPlayer is paused and that it should now show a play button in the remote control buttons bar?
Make sure that you're setting the MPNowPlayingInfoPropertyPlaybackRate property. 0.0f to indicate paused, 1.0f to indicate playing. You'll also need to set the MPNowPlayingInfoPropertyElapsedPlaybackTime when you change these values.
Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.
[self updateMetadata:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:audioFile.player.currentTime],
MPNowPlayingInfoPropertyElapsedPlaybackTime,
[NSNumber numberWithFloat:0.0f],
MPNowPlayingInfoPropertyPlaybackRate,
nil]];
I had this problem today: I found that the fact that I was recording audio as well as playing it caused the button to show the pause symbol.
When I stopped the AVAudioRecorder from recording the pause button became a play button.
Quite often, the problem is simply the iPhone Simulator. As soon as you are using the play() function of your AVAudioPlayer instance, the remote control bar is supposed to toggle pause/play automatically. If you run into problems where this doesn't happen, try to run your program on a device.
To toggle the button, you do not need to set any playingInfo of the MPNowPlayingInfoCenter, neither do you need to hold an active AVAudioSession.
Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.In swift
self.updateMetadata(NSDictionary.dictionaryWithValuesForKeys(NSNumber(Double(audioFile.player.currentTime))),MPNowPlayingInfoPropertyElapsedPlaybackTime,NSNumber(numberWithFloat:0.0f),MPNowPlayingInfoPropertyPlaybackRate,nil)

ios - AVAudioPlayer - audioPlayerDidFinishPlaying is sometimes called at random times in a clip

I have a ~30 minute mp3 file being played by an AVAudioPlayer. Sometimes, audioPlayerDidFinishPlaying:successfully is called (with the successfully flag set to TRUE), even when it is only 5 minutes into the clip. It happens infrequently, but it can happen at any position in the clip.
I've only noticed that it happens when the user is pressing a button on the user interface, or moving a slider. And the more quickly they press buttons on the user interface, the more likely it is to happen it seems.
Any ideas what could be causing this, or how to fix it?
MORE INFO:
Only 1 sound is played at a time. No sounds are played for button actions. The AVAudioPlayer is declared locally in my main ViewController. I've tested on an iPhone 3GS and an iPhone 4s. The problem happens very very rarely on the iPhone 4s. It's much more frequent on the 3GS.
OK, I had the same problem starting yesterday. Don't whether we were having the same problem but I will throw it here
In the slider ValueChanged event handler, I tried to update the view elements based on the new position of the slider. But within the updateViewMethod, I called another
slider.value = self.player.currentTime;
Removing that solved my problem.
So in short, your timer's callback should update the slider, but slider valuechanged handler should not redo the work.

Make sound stop when button is pressed iOS

I have multiple buttons that play sounds using AVAudioPlayer. If you press one, and press another before the first ends, they are both playing at the same time. How would i make it so if you press two buttons, only the last pressed button's sound plays?
I don't know the exact code, but you may have to bool something or try AudioServicesDisposeSystemSoundID. It will stop the sound immediately.
Before you start the new song you have to do:
[song1 stop];
song1.currentTime = 0;
And do that for all the songs that could be possibly playing. Then you can make the new song play. (The current time sets it to start from the beginning if you play it again.) hope that helps :)

How to keep MPMoviePlayerController open after movie ended?

If I use MPMoviePlayerController to play video in my iPhone app, it opens, loads the movie, plays it and then closes. Is it possible to force it to stay open after the movie finishes, so that user can replay it using its controls, instead of using controls in parent view? Also, is it possible to start MPMoviePlayerController in the paused mode?
thanks for any advice.
None of that is possible using the available API in the Pre-3.2 OS. One thing you could do is take a scerenshot of the last frame and stick that behind the movie so when it's done playing, it looks like it's still there, then just stick a button on it to replay. you could make an interface that looks/behaves similar to the standard movie player interface just for the play button if you need to.
For anyone stumbling over this now... There is a way to do this. Check out this question :)

Can't stop movie playback immediately

When you launch a video and IMMEDIATELY press "Done" the MPMoviePlayerController will exit, however the video still plays in the background (you can hear the audio). It works fine if you allow the video to begin playing before you hit the "Done" button.
Does anybody know a workaround for this?
Try calling stop on your MPMoviePlayerController instance when the view controller that spawned the movie receives its viewDidAppear: (or viewWillAppear:) methods
While, so far, I've also found MPMoviePlayerController somewhat erratic, I imagine just releasing the object (after listening to a MPMoviePlayerPlaybackDidFinishNotification) would help.
Update: This seems to be a known 3.0 bug. I was unable to find a workaround
http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/15768-mpmovieplayer-video-via-url-glitch.html