完成按钮事件MPMoviePlayerController

w00*_*w00 29 objective-c mpmovieplayercontroller ios

在我的iPhone上,我正在以全屏模式播放视频/音频文件.当视频/音频文件到达终点时,将触发以下方法:

- (void) movieFinishedCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *player = [aNotification object];

    [player stop];

    [[NSNotificationCenter defaultCenter] 
        removeObserver:self
        name:MPMoviePlayerPlaybackDidFinishNotification
        object:player];

    [player autorelease];
    [moviePlayer.view removeFromSuperview];

    NSLog(@"stopped?");
}
Run Code Online (Sandbox Code Playgroud)

这很好用!但问题是当视频/音频文件仍在播放时我按下"完成"按钮.那么这个方法不会被触发......

任何人都知道在按下"完成"按钮时如何捕捉事件?因为现在媒体播放器仍然在视图中.它并没有消失.

ber*_*ium 44

我听的时候它在iPad上适用于我 MPMoviePlayerWillExitFullscreenNotification.

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(doneButtonClick:) 
                                             name:MPMoviePlayerWillExitFullscreenNotification 
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

和选择器方法:

-(void)doneButtonClick:(NSNotification*)aNotification{
    NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    if ([reason intValue] == MPMovieFinishReasonUserExited) {
        // Your done button action here
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我的MPMovieplayer风格是FullScreen.现在点击完成按钮 - 视频只是暂停,它没有通知MPMoviePlayerWillExitFullscreenNotification.任何的想法? (2认同)

dkl*_*klt 21

检查通知userInfo字典中的枚举

NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

if ([reason intValue] == MPMovieFinishReasonUserExited) {

   // done button clicked!

}
Run Code Online (Sandbox Code Playgroud)

选定的答案已经整合了我的回复.请参考上面.

  • 并且观察者是:MPMoviePlayerPlaybackDidFinishNotification (5认同)

Kam*_*pai 17

在iOS7和iOS8中成功测试

检查并删除先前的通知观察者(如果有)MPMoviePlayerPlaybackDidFinishNotification.

- (void)playVideo:(NSString*)filePath
{
     // Pass your file path
        NSURL *vedioURL =[NSURL fileURLWithPath:filePath];
        MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];

    // Remove the movie player view controller from the "playback did finish" notification observers
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:playerVC.moviePlayer];

    // Register this class as an observer instead
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:playerVC.moviePlayer];

    // Set the modal transition style of your choice
    playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    // Present the movie player view controller
    [self presentViewController:playerVC animated:YES completion:nil];

    // Start playback
    [playerVC.moviePlayer prepareToPlay];
    [playerVC.moviePlayer play];
}
Run Code Online (Sandbox Code Playgroud)

解雇控制器

- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Obtain the reason why the movie playback finished
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
    {
        MPMoviePlayerController *moviePlayer = [aNotification object];

        // Remove this class from the observers
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        // Dismiss the view controller
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
Run Code Online (Sandbox Code Playgroud)

你做完了!!!

  • 谢谢!将`[self dismissModalViewControllerAnimated:YES];`更改为`[self dismissViewControllerAnimated:YES completion:nil];`以避免iOS 8上的弃用警告. (2认同)