Movie is played with some delay after calling play on MPMoviePlayerController - iphone

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.

Related

Difference between prepareToPlay and Play in MPMoviePlayerController

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

Playing the multiple Audio files in different Views using single audio player instance in iphone,

I am new to audio playing, I am having one issue regarding Audio playing .The issue is Playing the multiple audio files , and controlling the current playing audio file from other Views.
My Requirement is. In my app i am having 4 audio files in different Views. Once the audio play button is clicked in some View, The audio playing has to be started and pause and stop and slider buttons has to be displayed , Even i navigated to the next view the audio has to be continued (I should be able to control the currently playing audio), If i pressed the navigated view's audio, The previous audio has to be stopped, Navigated page audio has to be played. After completion of Navigated page Audio, the resumed audio of Previous page has to be played.
As of my knowledge, I have take single audio instance in AppDelegate. I am referring that Audioplayer instance in all view's , Where ever i need. It is not working.(some times giving error , some times unable to stop the Previous Audio playing).
Thanks in advance.
if i am not wrong you have defined the audioplayer in appdelegate, right?
and which class are you using for this , avplayer , avaudioplayer or mpplayer ?
and by the way , if we come to answer of mine,
i have used avplayer and avaudioplayer for this kind of business and it worked without problem , have you checked that in everyview you initialize you appdelegate instance? if not please check this and
also in your plist file , you should state that app will play audio in background to get the support for that.
hope this answer will help! :)

How to know when AVPlayer uses the audio only bit-rate?

While streaming when encountering slow connectivity the AVPlayer may choose to play the lowest bit-rate in the HTTP Live Streaming playlist.
Is there a way to identify this transition?
I've tried observing the AVPlayerItem "tracks" property via KVO to see when it contains only audio but in most cases the tracks property isn't changed even though the player switched to the audio only stream.
I found out that the AVPlayerItem tracks property was not dependable on the simulator but somewhat more dependable on the actual device (with a ~5 seconds deviation).
Whenever the tracks property changes (you can find out when via KVO) you should traverse the tracks and see if there are any tracks with 'mediaType' set to AVMediaTypeVideo.
If there are none then you can conclude that you are in an audio only state.

How to play more than one video in cocoa touch?

I'm trying to play more than one video at the same time in Cocoa Touch.
I can play one video with MPMoviePlayerController, but it does not allow to play more than one: "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."
Basically I'm trying to display complex, high resolution animation in a loop with autostart, but using PNGs makes the app size way too big (+500 MB)
I converted one object of the animation to a movie which is about 50x smaller now.
I think its not possible to play two video at the same time. But you can play video in queue refer this article....

Find if an audio is currently playing

I need to find out if my program is currently playing any audio and in case it does, I want to stop the previous audio and start a new playback.
The property kAudioSessionProperty_OtherAudioIsPlaying always returns a 0 (probably only checks whether iPod music is playing)
There's another property kAudioQueueProperty_IsRunning but this always returns a 0 whether the audio is running or not. Can someone please tell me how I can find out if an audio is playing in my app or not.
Thanks.
Note: The class from which I invoke my streamer gets deallocated when I move back in the view hierarchy. So I do not have any way of accessing the AudioFileStreamID to know whether audio is playing. I need to use one of the properties provided by the SDK.
Found a workaround. Instead of creating the streamer object in a viewcontroller, I am using a reference in the appDelegate. This way I'll always have a live reference of the streamer class which I can access in any of the viewcontrollers to know whether audio is currently playing or not.