1. 程式人生 > >MPMovieplayerviewcontroller播放結束後自動消失的解決方案

MPMovieplayerviewcontroller播放結束後自動消失的解決方案

你可以使用這段程式碼來阻止控制器播放在播放結束後自動dismissing(消失),並且捕捉到使用者點選完成的按鈕事件去自己定義並處理讓你的MPMoviePlayerViewController播放器的消失(dismiss)的時機

步驟 1. - 建立並初始化一個MPMoviePlayerViewController(videoPlayer)

MPMoviePlayerViewController *videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[[NSURL alloc ]initWithString:[aURL];

步驟 2. - 移除videoPlayer預設的通知事件並且加入自己的通知事件。

[[NSNotificationCenter defaultCenter] removeObserver:videoPlayer
name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer]
;

步驟 3. - 手動顯示你的videoPlayer控制器

[self presentMoviePlayerViewControllerAnimated:videoPlayer];

步驟 4. - 新增 videoFinish: 方法處理通知事件

-(void)videoFinished:(NSNotification*)aNotification{
    int value = [[aNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if
(value == MPMovieFinishReasonUserExited) { [self dismissMoviePlayerViewControllerAnimated]; } }