I want to get current video playing time, not total duration.
My code is
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(MPMoviePlayerLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {
if ((self.moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
NSLog(@"content play length is %g seconds", self.moviePlayer.duration);
// self.lblVideoDuration.text = [NSString stringWithFormat:@"%@", self.moviePlayer.duration];
}
}
but above will give me total duration, but i don't want total duration.
if my Video duration is 1 minute and currently it play around 30 seconds, i want that live playback timing.
how can i get that?
I am able get Video Playback timing by start timer like this.
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {
if (self.moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {
[self.activityIndicator stopAnimating];
[self.activityIndicator removeFromSuperview];
[self startDurationTimer];
}
}
- (void)startDurationTimer {
self.durationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(monitorMoviePlayback) userInfo:nil repeats:YES];
}
- (void)stopDurationTimer {
dispatch_async(dispatch_get_main_queue(), ^{
[self.durationTimer invalidate];
self.durationTimer = nil;
});
}
- (void)monitorMoviePlayback {
double currentTime = floor(self.moviePlayer.currentPlaybackTime);
double totalTime = floor(self.moviePlayer.duration);
double minutesElapsed = floor(currentTime / 60.0);
double secondsElapsed = fmod(currentTime, 60.0);
NSLog(@"Minute = %f && Second = %f", minutesElapsed, secondsElapsed);
}
}
but when i call stoptimer method , Timer is not invalidating or not stopped.
how can i invalidate Timer?



If it is an option for you to use
AVPlayerViewControllerinstead ofMPMoviePlayerViewController, you can get current time from it viaAlthough it is recommended to switch to AVPlayerViewController, as MPMoviePlayerViewController is deprecated, to get current time from MPMoviePlayerViewController, you can use following code: