Search code examples
iphoneobjective-cvideoxcode4.3mpmovieplayer

Objective-C: Playing video by frame


I'm very new to iPhone development and I'm making an application where I have to play video, what I would like to do is to play a certain number of frames once an event is risen. Most of that is already done, but I couldn't achieve yet, is how to play only 50 frames exactly, when I try to calculate how long 50 frames is by seconds, I'm able to make play/pause but after sometime, things starts to be wrong as the calculating is not 100% correct

Please if someone can help, I will be very glade to receive any advice/remark/critics, thank you in advance :)

P.S: I'm using MPMoviePlayerController to play my video

Regards

EDIT: If you know any other methods that can do what I need other than MPMoviePlayerController I can use it with no problem


Solution

  • after a long research I couldn't find what I'm searching about (don't know even if it exists), so the I was thinking of a solution for this and following are the solutions I found:

    1. Split the video file to sequences that I play only the the sequence I need.
    2. See the difference in time between what I wanted to play and what it was really played than correct it in the next turn.

    For me when I tried to split the video, I lost quality, so I used the second method which I'm describing bellow:

    I needed to play 20 frames out of 720, my frame rate is 25 frames/second, after a basic calculating I could find that 20 frames is played in 0.8 second. lets say the "wait" variable is the duration (which is 0.8 for my case, but needs some correction) I would like to play (I will put how to calculated it later)

    First, I had defined a static "timer" in the class as:

        static NSTimer *timer;
    

    Because I'm playing in loop, I need to reste my timer every time I need to play a sequence (just to be sure), than I recreate it as new and set the function which will be called to pause the video (pauseVideoAtAnimation):

        [timer invalidate];
        timer = [NSTimer scheduledTimerWithTimeInterval:wait target:self selector:@selector(pauseVideoAtAnimation:) userInfo:nil repeats:NO];
    

    Than I play the video:

        [player.moviePlayer play]
    

    after that all we need to add is the instruction in the pauseVideoAtAnimation to pause the video

        [self.player.moviePlayer pause];
    

    All of that I have done it before even asking for help, so what changes in the correction of the wait variable, following is the instruction for that (here we need a counter which starts from 0, and counts how many time we played a sequence)

         double wait = 0.8;
         wait += (counter * wait - self.player.moviePlayer.currentPlaybackTime);
         counter ++;
    

    and thats it, if any one have the same problem or just curious about the solution, don't hesitate to contact me :)