0

I’m making a video that shows overlays at certain intervals of a video. I’ve managed to get the video to play. Now my objective is to watch/observe the currentPlaybackTime value and pause the video when it hits 2 seconds.

After some research found that currentPlaybackTime does not support KOV. So I need to implement this solution but I have no idea where to put the code - I’m very new to Objective C. I keep trying to put it in the ViewController (My only view) but the way its written hints to it being placed somewhere else…

Should I create a new controller for it? Or do I override methods from the MediaPlayer framework?

Here's my code:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) MPMoviePlayerController *movieController;

@property(nonatomic) NSTimeInterval currentPlaybackTime;

@end



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidAppear:(BOOL)animated {

    self.movieController = [[MPMoviePlayerController alloc] init];


    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

    [self.movieController.view setFrame:CGRectMake (0, 0, 480, 326)];
    [self.view addSubview:self.movieController.view];


    [self.movieController play];
    [self.movieController currentPlaybackTime];




}



@end
Community
  • 1
  • 1
Dol
  • 944
  • 3
  • 10
  • 25

1 Answers1

2

You can implement following method in your found solution in this same view controller.

Right after

[self.movieController play];

call following method

[self BeginPlayerPolling];

Register this class as an observer before in your viewDidAppear where you initialised your movieController

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object: self.movieController];
self.movieController = [[MPMoviePlayerController alloc] init];

and implement this notification observer's method

- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Remove the movie player view controller from the "playback did finish" notification observers
    [[NSNotificationCenter defaultCenter] removeObserver: self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object: self.movieController];
    [self EndPlayerPolling];
}
ldindu
  • 4,270
  • 2
  • 20
  • 24
  • Hi, your help is very much appreciated :). Unfortunately, I'm getting the error "use of undeclared identifier movieController" when registering the class as an observer in viewDidAppear. Which is odd because I thought I declared it in the header file. – Dol Dec 27 '13 at 22:39
  • Try now, I made a mistake of accessing movieController as ivar instead of property. Hope it will work now. – ldindu Dec 27 '13 at 22:48
  • Ok so no errors for now :), thanks. So do I not use the code from the link? Can I use your code to observe the currentPlaybackTime value? – Dol Dec 27 '13 at 23:07
  • 1
    Observer has been added to track when the movie player completed playing movie. You still need those BeginPlayerPolling and EndPlayerPolling to track currentPlaybackTime's value. – ldindu Dec 27 '13 at 23:10
  • Thanks, I added them now, just a few errors in them.. Shall I declare pollPlayerTimer as follows: `@property(nonatomic) NSTimeInterval pollPlayerTimer;` ? – Dol Dec 27 '13 at 23:21
  • 1
    As pollPlayerTimer appeared to be a time, it should be declared as follow @property(nonatomic, strong) NSTimer *pollPlayerTimer; – ldindu Dec 27 '13 at 23:47