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

来源:互联网 发布:赵丹喵 知乎 编辑:程序博客网 时间:2024/05/06 08:22

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对UIApplicationDidEnterBackgroundNotificationMPMoviePlayerPlaybackDidFinishNotification      通知的监听,并实现自己的监听方法

复制代码
// 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];
}
复制代码

 

原创粉丝点击