可以使用通知中心:
1-論viewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackStateDidChange:)
name:@"MPAVControllerPlaybackStateChangedNotification"
object:nil];
2-實現此方法(秒是int):
- (void)playbackStateDidChange:(NSNotification *)note {
NSLog(@"note.name=%@ state=%d", note.name, [[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue]);
if ([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 2) {
timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(increaseSeconds) userInfo:nil repeats:YES];
NSLog(@"seconds: %i", seconds);
} else if([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 1){
[timer invalidate];
NSLog(@"seconds: %i", seconds);
} else if([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 0){
NSLog(@"Total watched: %i", seconds);
[self dismissMoviePlayerViewControllerAnimated];
}
}
MPAVControllerNewStateParameter == 2(視頻開始) MPAVControllerNew StateParameter == 1(視頻停止) MPAVControllerNewStateParameter == 0(視頻結束或者按 「完成」)
3-最後實現此方法:
-(void) increaseSeconds {
seconds++;
}
如果什麼互聯網是緩慢和2 mintues視頻拿5 mintues發揮 – user3110080
我編輯我的答案,請檢查並讓我知道。 – Suryakant