3

I use MPMoviePlayer to play my video, the problem I have is when the user press the home button of the device, I save the current position of the video, than when he lunch the application again I would like him to start from where he left, so I use setCurrentPlaybackTime function, it works perfectly in iOS 5 but not in iOS 5.1 where the video starts from the beginning.

Does any body have a solution for this kind of problem ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abu Romaïssae
  • 3,841
  • 5
  • 37
  • 59
  • How are you storing the time? Maybe that is the problem.... and nto the setCurrentPlaybackTime. Check what value you store in 5.1. – Bo. May 17 '12 at 12:46
  • I'm storing it in an attribute of a Class with a singelton pattern, but the time is correct I user **NSLog()** to check it and seems to have the correct value – Abu Romaïssae May 17 '12 at 13:05

3 Answers3

7

My problem is solved and I will explain what I've done so far.

When I couldn't find a solution/explanation of that behavior (which I find it strange) I start making lot and lot of logging, so what I noticed is when I log the current position I get always (in iOS 5.1)

  currentPlaybackTime = nan

but in iOS 5 I got a normal value as 0.00000

so I made a timer (which repeats itself) to log every time the current time, so I noticed that its changing from nan to 0.00000 after a while

The conclusion I made is that I need to wait a bit (don't understand why) before setting the PlaybackTime, and so it is, after waiting 1 ms (1/1000 s) and it works.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abu Romaïssae
  • 3,841
  • 5
  • 37
  • 59
2

Did you try using seekToTime (AvPlayer only) instead of setCurrentPlaybackTime?

as suggested here:
Why does MPMoviePlayerController setCurrentPlaybackTime goes to the wrong time?

Note: This solution is for AvPlayer rather than MPMoviePlayer (even tough it is not indicated in the listed link). But I would wait for additional answers before switching to AvPlayer.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Bo.
  • 2,547
  • 3
  • 24
  • 36
  • I tried right now, but it doesn't seem to know the function **seekToTime** and it gives me an error while building, or did I made something wrong ? – Abu Romaïssae May 17 '12 at 13:31
  • This the complication error message I get: No visible @interface for 'MPMoviePlayerController' declares the selector 'seekToTime:' – Abu Romaïssae May 17 '12 at 13:57
  • I apologize for not stating initially that answer relies on switching to AvPlayer from MPMoviePlayer (I checked the post and yes it is not obvious when reading it initially). – Bo. May 17 '12 at 14:02
  • hum.. ok, so I can't use it in this case, I'have done lot of things with the MPMoviePlayer and I'm not ready to repeat all, also I'm not sure if I could do it in time, any way **thank you** for your help :) . What disturbs me most is that every thing is working fine for iOS 5 :( – Abu Romaïssae May 17 '12 at 14:04
0

We should set the currentPlaybackTime after we get the MPMoviePlayerLoadStateDidChangeNotification notification.

To know whether the moviePLayer is loaded, first you need to register for MPMoviePlayerLoadStateDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

Once the movie player is loaded movieLoadStateDidChange: will be called, then check for MPMovieLoadStatePlaythroughOK load state and set the currentPlaybackTime.

- (void)movieLoadStateDidChange:(NSNotification *)notification
{
    MPMoviePlayerController *player = notification.object;
    MPMovieLoadState loadState = player.loadState;

    /* Enough data has been buffered for playback to continue uninterrupted. */
    if (loadState & MPMovieLoadStatePlaythroughOK)
    {
        //should be called only once , so using self.videoLoaded - only for  first time  movie loaded , if required. This function wil be called multiple times after stalled
        if(!self.videoLoaded)
        {
            self.moviePlayerController.currentPlaybackTime = self.currentPlaybackTime;
            self.videoLoaded = YES;
        }
    }
}
Pradeep HD
  • 11
  • 3