您的位置:首页 > 产品设计 > 产品经理

iOS 视频播放(一) MPMoviePlayerViewController、避免在后台销毁

2016-09-14 10:36 525 查看
转自:http://blog.csdn.net/xueer8835/article/details/22286629

#import <MediaPlayer/MediaPlayer.h>
MediaPlayer.framework。

MPMoviePlayerViewController:
打开网络视频:
-(void)openmovie
{
MPMoviePlayerViewController *movie = [[MPMoviePlayerViewControlleralloc]initWithContentURL:[NSURLURLWithString:@"视频网络地址"]];

[movie.moviePlayer prepareToPlay];
[self presentMoviePlayerViewControllerAnimated:movie];
[movie.moviePlayersetControlStyle:MPMovieControlStyleFullscreen];

[movie.viewsetBackgroundColor:[UIColorclearColor]];

[movie.view setFrame:self.view.bounds];
[[NSNotificationCenterdefaultCenter]addObserver:self

selector:@selector(movieFinishedCallback:)

name:MPMoviePlayerPlaybackDidFinishNotification

object:movie.moviePlayer];

}
-(void)movieFinishedCallback:(NSNotification*)notify{

// 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Done按钮被点击响应的通知。

MPMoviePlayerController* theMovie = [notifyobject];

[[NSNotificationCenterdefaultCenter]removeObserver:self

name:MPMoviePlayerPlaybackDidFinishNotification

object:theMovie];

[selfdismissMoviePlayerViewControllerAnimated];

}
打开本地视频:
-(void)openmovie
{

NSString *url = [[NSBundlemainBundle]pathForResource:@"IMG_0322"ofType:@"mp4"];
MPMoviePlayerViewController *playerViewController  = [[MPMoviePlayerViewControlleralloc]initWithContentURL:[NSURLfileURLWithPath:url]];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewControllermoviePlayer]];

[self.view addSubview:playerViewController.view];

MPMoviePlayerController *player = [playerViewControllermoviePlayer];
[playerplay];

}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotificationobject];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:MPMoviePlayerPlaybackDidFinishNotificationobject:player];
[playerstop];
[player.viewremoveFromSuperview];

}


避免MPMoviePlayerViewController播放完毕后自动dismiss,进入后台自动dismiss

MPMoviePlayerViewController已经实现了一些通知的监听并对MPMoviePlayerController实现了一些控制,比如:

 

1. 监听UIApplicationDidEnterBackgroundNotification通知,调用[movieplayer stop],播放器停止。

2. 监听MPMoviePlayerPlaybackDidFinishNotification(调用stop方法或视频播放结束时发送通知)通知,调用dismiss方法移除自身。

 

需求1:app中一个课程包含若干个章节,所以每次播放完一个章节后要求直接加载播放下一个课程。

遇到问题:由于MPMoviePlayerViewController监听了MPMoviePlayerPlaybackDidFinishNotification通知,当一个视频播放完毕,它会在监听方法中       调用dismissMoviePlayerViewControllerAnimated方法,播放器视图就直接移除了。

解决方法:

// self为MPMoviePlayerViewController的一个实例对象。
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];


 

需求2: app进入后台时播放器暂停,回到应用时继续播放

遇到问题:由于MPMoviePlayerViewController监听了UIApplicationDidEnterBackgroundNotification通知,当进入后台时,调用stop方法,随后接      收到MPMoviePlayerPlaybackDidFinishNotification通知,调用dismiss方法移除自身视图。

解决方法:移除MPMoviePlayerViewController对UIApplicationDidEnterBackgroundNotification和MPMoviePlayerPlaybackDidFinishNotification      通知的监听,并实现自己的监听方法



// UIApplicationDidEnterBackgroundNotification通知
- (void)appEnterBackground:(NSNotification*)notice
{
  // 进入后台时记录当前播放时间
overlay_flags.playTimeWhenEnterBackground = _player.currentPlaybackTime;
[_player pause];
}

// UIApplicationWillEnterForegroundNotification通知
- (void)appEnterForeground:(NSNotification*)notice
{
  // 设置播放速率为正常速度,设置当前播放时间为进入后台时的时间
[_player setCurrentPlaybackRate:1.0];
[_player setCurrentPlaybackTime:overlay_flags.playTimeWhenEnterBackground];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐