Difference between prepareToPlay and Play in MPMoviePlayerController - iphone

Please let me know the difference between prepareToPlay & play Methods in MPMoviePlayerController at the time of Video Play.
The syntax are:
[moviePlayer prepareToPlay];
And
[moviePlayer play];

As Method say that
prepareToPlay - is not starting to play but it is under process for play whatever (video/audio).
play - says that it is do string to play whatever (video/audio).
as Document say:
play
Initiates playback of the current item. (required)
- (void)play
Discussion
If playback was previously paused, this method resumes playback where it left off; otherwise, this method plays the
first available item, from the beginning.
If a movie player is not prepared for playback when you call this
method, this method first prepares the movie player and then starts
playback. To minimize playback delay, call the prepareToPlay method
before you call this method.
To be notified when a movie player is ready to play, register for the
MPMoviePlayerLoadStateDidChangeNotification notification. You can then
check load state by accessing the movie player’s loadState property.
Availability Available in iOS 3.2 and later. Declared In
MPMediaPlayback.h
prepareToPlay
Prepares a movie player for playback. (required)
- (void)prepareToPlay
Discussion
If a movie player is not already prepared to play when you
call the play method, that method automatically calls this method.
However, to minimize playback delay, call this method before you call
play.
Calling this method may interrupt the movie player’s audio session.
For information on interruptions and how to resond to them, see Audio
Session Programming Guide.
Availability Available in iOS 3.2 and later.
Declared In MPMediaPlayback.h
For More Information read This Official Documentation.

To minimise playback latency by performing expensive operations upfront.
In order to play back a multimedia file, such as a QuickTime movie, there is a non-trivial amount of loading and processing required before the file can actually be played. Having separate play and prepareToPlay methods allows the developer to choose when potentially expensive operations involved with playback can be performed, to minimise the delay when the user actually presses the play button.
For example, the header needs to be read and parsed, and metadata extracted. The chapter index might need to be read, and the player might need to seek to the end of the file to read chunk offset tables, read thumbnails, poster frames and and many more. Also, to enable rapid playback when the user presses play, the system probably wants to load, uncompress and cache the first second or so of audio and video content. All of this can take a noticeable amount of time, and would be performed by the prepareToPlayback method.
Given the above, the play method can immediately start to play the multimedia content when the user nominates. Obviously, if the media hasn't already been prepared, the system would call prepareForPlayback for you at the start of play to perform these necessary preparations.
In your app, for example, the user might select a multimedia clip in one step. You could call prepareToPlay right away, and show the poster frame in the preview window. Then when the user presses the Play> button, the content is ready to go.
A simplistic parallel in the analog world might be something akin to threading the tape into a spool, winding up the spool and pretensioning the tape, positioning the tape head at the start of the content. Then when you press Play, the sound is heard almost immediately.

prepareToPlay
Prepares a movie player for playback. (required) If a movie player is
not already prepared to play when you call the play method, that
method automatically calls this method. However, to minimize playback
delay, call this method before you call play.
play
Initiates playback of the current item. (required) If playback was
previously paused, this method resumes playback where it left off;
otherwise, this method plays the first available item, from the
beginning. If a movie player is not prepared for playback when you
call this method, this method first prepares the movie player and then
starts playback. To minimize playback delay, call the prepareToPlay
method before you call this method.
Please visit MPMediaPlayback Protocol Reference

Related

For how long does the AVPlayer continues to buffer from an URL after a pause?

I was reading the AVPlayer class documentation and I couldn't find the answer for my question.
I'm playing a streamed audio from the Internet on my iPhone app and I'd like to know if after a [myAVPlayer pause]; invocation myAVPlayer will keep downloading the audio file on the background for a long time.
If the user pushes the "Pause" button, invoking [myAVPlayer pause]; and then leaves the app, will myAVPlayer keep downloading a large amount of data?
I'm concerned about this when the user is on 3G Network.
I am faced with the same question and have done some experimentation. My observations are only valid for video, but if they are also valid for audio, then AVPlayer will try to buffer around 30s of content when you press pause. If you have access to the webserver, you could run tcpdump/wireshark and see how long after you press pause that the server continues to send data.
You can manage how long AVPlayer continues to buffer.
You need to manage preferredForwardBufferDuration of avplayer currentItem. If you want to stop buffering set value to 1 because if you set it to 0 it will be set up automatically
self.avPlayer.currentItem.preferredForwardBufferDuration = 1;
From Apple documentation: This property defines the preferred forward buffer duration in seconds. If set to 0, the player will choose an appropriate level of buffering for most use cases. Setting this property to a low value will increase the chance that playback will stall and re-buffer, while setting it to a high value will increase demand on system resources.

Movie is played with some delay after calling play on MPMoviePlayerController

I am developing an iPhone application in which I play videos using MPMoviePlayerController.
Sometimes, some of the videos don't play immediately after I call play on MPMoviePlayerController.
I have called prepareToPlay and in the notified method of MPMediaPlaybackIsPreparedToPlayDidChangeNotification, I am calling play on MPMoviePlayerController.
Could someone help in identifying the problem here?
Thanks,
Laxmilal
From my answer in a similar thread (reducing-the-initial-delay-when-playing-remote-video-content) - Note this fragment of the solution is valid for both, remote and local video content.
Use theMPMoviePlayerController.movieSourceTypeproperty when initializing your
player to cut down the media
recognition delay.
From the MPMoviePlayerController Class Reference:
The default value of this property is
MPMovieSourceTypeUnknown. This
property provides a clue to the
playback system as to how it should
download and buffer the movie content.
If you know the source type of the
movie, setting the value of this
property before playback begins can
improve the load times for the movie
content. If you do not set the source
type explicitly before playback, the
movie player controller must gather
this information, which might delay
playback.

AVAudioPlayer lag when calling play

I have set up an AVAudioPlayer object in viewDidLoad, as per the Apple guidelines, calling prepareToPlay as the last line in viewDidLoad (i have tried in awakeFromNib also).
When i press my play button, there is a pause, as it would appear to load the file, then it plays.
In audioPlayerDidFinishPlaying, i reload the player with a different sound, and when clicking play for a second time, the file plays instantly.
What would cause the player to lag on the first play?
Thanks.
The delay is due to AVAudioPlayer being initialised. Please see this answer.
The audio system runs on several asynchronous software processes (audio units, OS drivers, etc.) and hardware systems (DMA, DACs, audio amp power supplies, etc.) that never really all completely finish initialization until some sound is actually played all the way out the speakers or earphones.
Here's one method to do that: Create a sound file containing a half second of silence. On app start up, while your app and view controller are still loading, use AVAudioPlayer to play this file of silence. Now when your view finishes loading, AVAudioPlayer should be ready to play subsequent non-silent sounds much faster, since some audio (silence) has already already gone all the way out to the speakers.
What kind of sound are you playing? Alerts, something longer? If alerts, I did go this way and it's much better with lags ...
create system sound with AudioServicesCreateSystemSoundID
play system sound with AudioServicesPlaySystemSound
dispose system sound with AudioServicesDisposeSystemSoundID
... you only need to store SystemSoundID for each sound you would like to play.

MPMoviePlayerViewController method click on pause

I'm using MPMoviePlayerViewController and I want to know if there is a method that can tell me when the user clicks on the Pause button for the video, like an observer?
You should use MPMoviePlayerController instead :
Movie Player Notifications
The MPMoviePlayerController class generates numerous notifications to keep your application informed about the state of movie playback. In addition to being notified when playback finishes, interested clients can be notified in the following situations:
When the movie player begins playing, is paused, or begins seeking forward or backward
When the scaling mode of the movie changes
When the movie enters or exits fullscreen mode
When the load state for network-based movies changes
When meta information about the movie itself becomes available
For more information, see the Notifications section in this document.
http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html

Marking an MPMediaItem (podcast) as listened to

Is there a way to update the 'listened to' status of an MPMediaItem from the iPod library?
I've tried using MPMusicPlayerController in iPodMusicPlayer mode and it does not mark the podcast as listened to once it finishes playing.
As far as I can see it's not possible to change media attributes from MPMusicPlayer controller.
The only thing you could try play back the podcast and jump to the last second, then play back the last second (and turn the volume to 0 for that second).
But this is no good way as well because, you have play back something this causes a stop of every other iPod playback.