iOS 視訊播放(一) MPMoviePlayerViewController、避免在後臺銷燬
轉自: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已經實現了一些通知的監聽並對MPMoviePlayerController實現了一些控制,比如:
1. 監聽UIApplicationDidEnterBackgroundNotification通知,呼叫[movieplayer stop],播放器停止。
2. 監聽MPMoviePlayerPlaybackDidFinishNotification(呼叫stop方法或視訊播放結束時傳送通知)通知,呼叫dismiss方法移除自身。
需求1:app中一個課程包含若干個章節,所以每次播放完一個章節後要求直接載入播放下一個課程。
遇到問題:由於MPMoviePlayerViewController監聽了MPMoviePlayerPlaybackDidFinishNotification通知,當一個視訊播放完畢,它會在監聽方法中 呼叫dismissMoviePlayerViewControllerAnimated方法,播放器檢視就直接移除了。
解決方法:
// self為MPMoviePlayerViewController的一個例項物件。
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotificationobject: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];
}