MPMoviePlayerViewController完成按钮不起作用

时间:2015-03-26 21:58:48

标签: ios mpmovieplayercontroller

我有一个地方允许用户从UIImagePicker中选择一个视频,然后通过MPMoviePlayerViewController播放。这样可以正常工作,除非按下完成按钮时它只是暂停视频并且不会从全屏幕中将其关闭。我已经被认为完成按钮的默认行为是关闭视频,所以我不确定是什么阻止它做这件事,或者我可以改变做什么它发生了。这是我目前的代码,我已经尝试了一些改动,我会在代码后记下这些代码。

NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if(CFStringCompare((CFStringRef) mediaType,  kUTTypeMovie, 0) == kCFCompareEqualTo)
{
    videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
}

moviePlayer =  [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[moviePlayer.view setTranslatesAutoresizingMaskIntoConstraints:YES];
moviePlayer.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[self.view addSubview:moviePlayer.view];

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerDidExitFullscreenNotification
                                              object:nil];

[moviePlayer.moviePlayer stop];
[moviePlayer.view removeFromSuperview];
}

我已尝试将[self.view addSubview:moviePlayer.view];更改为[self presentMoviePlayerViewControllerAnimated:];,但这并没有做任何事情。我已经提出了几个不同的变体:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerDidExitFullscreenNotification
                                           object:moviePlayer];

使用其余的初始化代码,但也没有帮助。

编辑:如果允许视频继续播放直到结束它也不会被解雇,那么它只是全屏停留在视频播放器中。

2 个答案:

答案 0 :(得分:0)

添加观察者时,object参数应该是moviePlayer.movi​​ePlayer而不仅仅是moviePlayer。

moviePlayer.movi​​ePlayer(类MPMoviePlayerController)是发送通知的人,而不是moviePlayer(类MPMoviePlayerViewController)。

所以改变这个:

[[NSNotificationCenter defaultCenter] addObserver:self
       selector:@selector(moviePlayBackDidFinish:)
       name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

到此:

[[NSNotificationCenter defaultCenter] addObserver:self
       selector:@selector(moviePlayBackDidFinish:) 
       name:MPMoviePlayerPlaybackDidFinishNotification moviePlayer.moviePlayer];

答案 1 :(得分:0)

对于Swift 3,这对我有用:

//register the observer
override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(CameraSetupViewController.moviePlayerDoneButtonClicked), name: NSNotification.Name.MPMoviePlayerPlaybackDidFinish, object: nil)

        moviePlayerPlay()
}

//moviePlayer play function
func moviePlayerPlay() {
     let url:NSURL = NSURL( string: "https://tendinsights.com/Video.mp4" )

     if let movieViewController = MPMoviePlayerViewController( contentURL: url as URL! ) 
           self.presentMoviePlayerViewControllerAnimated(movieViewController)
                movieViewController.moviePlayer.play()       
     }
}

//moviePlayer Done Button Pressed
func moviePlayerDoneButtonClicked(_ notification: NSNotification) {        
    let reason = notification.userInfo?[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]

    if (MPMovieFinishReason(rawValue: reason as! Int) == MPMovieFinishReason.userExited) {
        self.dismiss(animated: true, completion: nil)
    }
}