首页 > 代码库 > 使用MediaPlayer播放视频
使用MediaPlayer播放视频
使用MediaPlayer播放视频
by 伍雪颖
- (IBAction)userPlayerController:(id)sender { if (_moviePlayer == nil) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished4MoviePlayerController:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClick:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]]; _moviePlayer.scalingMode = MPMovieScalingModeAspectFill; _moviePlayer.controlStyle = MPMovieControlStyleFullscreen; [self.view addSubview:_moviePlayer.view]; } [_moviePlayer play]; [_moviePlayer setFullscreen:YES animated:YES]; } - (void)playbackFinished4MoviePlayerController:(NSNotification *)notification { [[NSNotificationCenter defaultCenter] removeObserver:self]; [_moviePlayer stop]; [_moviePlayer.view removeFromSuperview]; _moviePlayer = nil; } - (void)doneButtonClick:(NSNotification *)aNotification { if (_moviePlayer.playbackState == MPMoviePlaybackStateStopped) { [_moviePlayer.view removeFromSuperview]; _moviePlayer = nil; } } - (IBAction)userPlayerViewController:(id)sender { if (_moviePlayerView == nil) { _moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[self movieURL]]; _moviePlayerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill; _moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyleEmbedded; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished4MoviePlayerViewController:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; } [self presentMoviePlayerViewControllerAnimated:_moviePlayerView]; } - (void)playbackFinished4MoviePlayerViewController:(NSNotification *)notification { [[NSNotificationCenter defaultCenter] removeObserver:self]; [_moviePlayerView dismissMoviePlayerViewControllerAnimated]; _moviePlayerView = nil; } - (NSURL *)movieURL { NSBundle *bundle = [NSBundle mainBundle]; NSString *moviePath = [bundle pathForResource:@"xx" ofType:@"mp4"]; if (moviePath) { return [NSURL fileURLWithPath:moviePath]; }else { return nil; } }
使用MediaPlayer播放视频
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。