首页 > 代码库 > 获取视频停止播放时已经播放的时间
获取视频停止播放时已经播放的时间
获取停止,暂停时视频已经播放的时间主要靠注册通知,当暂停或停止时响应通知时间,获取当前已经播放的时间,同时也可以在视频开始播放时给一个播放时间,从当前播放时间播放视频,用此方法来达到播放视频从上次播放时间开始播放
主要代码
MPMoviePlayerViewController *mPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.itemVideoUrl]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mPlayer.moviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:mPlayer.moviePlayer];#pragma mark - 视频停止播放的通知,用来获取视频停止播放时已经播放的时间- (void)playbackDidFinish:(NSNotification *)aNote{ MPMoviePlayerController *moviePlayer = [aNote object]; NSDictionary *userInfo = [aNote userInfo]; int reason = [[userInfo objectForKey:@"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] intValue]; switch (reason) { case MPMovieFinishReasonPlaybackEnded: break; case MPMovieFinishReasonPlaybackError: break; case MPMovieFinishReasonUserExited: break; default: break; } CGFloat playtime = [moviePlayer currentPlaybackTime]; NSLog(@"%f", playtime); [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];}- (void)playbackStateDidChange:(NSNotification *)aNote{ MPMoviePlayerController *moviePlayer = [aNote object]; switch (moviePlayer.playbackState) { case MPMoviePlaybackStateStopped: NSLog(@"self.moviePlayer Stopped"); break; case MPMoviePlaybackStatePlaying://设置开始播放视频时的时间 moviePlayer.currentPlaybackTime = 300.07; NSLog(@"self.moviePlayer Playing"); break; case MPMoviePlaybackStatePaused: NSLog(@"self.moviePlayer Paused"); break; case MPMoviePlaybackStateInterrupted: NSLog(@"self.moviePlayer Interrupted"); break; case MPMoviePlaybackStateSeekingForward: NSLog(@"self.moviePlayer Forward"); break; case MPMoviePlaybackStateSeekingBackward: NSLog(@"self.moviePlayer Backward"); break; default: break; } CGFloat playtime = [moviePlayer currentPlaybackTime]; NSLog(@"%f", playtime);}
1.视频什么时候开始播放
当视频开始播放时会发送MPMediaPlaybackIsPreparedToPlayDidChangeNotification,所以你只需要在注册
MPMediaPlaybackIsPreparedToPlayDidChangeNotification通知,当接收到此通知说明视频开始播放。
2.自定义播放器视图时怎么自定义声音控件
使用MPVolumeView控制系统的声音大小。通过改变MPVolumeView slider的值可以改变系统的声音,同时通过调节系统的声音按钮,slider的值也会跟着改变。
volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(310, 180, 100, 20)];
volumeView.showsVolumeSlider = YES;
[backView addSubview:volumeView];
[volumeView release];
3.通过presentModalViewController调出MPMoviePlayer所在控制器,当通过dismissModalViewController移除控制器时,手机的状态栏可能会消失。解决办法:在dismissModalViewController调用函数
movieplayer.controlStyle = MPMovieControlStyleFullScreen;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[UIApplication sharedApplication].statusBarHidden = NO;
});
获取视频停止播放时已经播放的时间