【问题标题】:MPMoviePlayerViewController done button not workingMPMoviePlayerViewController 完成按钮不起作用
【发布时间】:2017-02-16 08:26:40
【问题描述】:

我有一个地方允许用户从 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];

使用其余的初始化代码,但这也无济于事。

编辑:如果允许视频一直播放到最后,它也不会关闭,它只是全屏卡在视频播放器中。

【问题讨论】:

    标签: ios mpmovieplayercontroller


    【解决方案1】:

    添加观察者时,对象参数应该是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];
    

    【讨论】:

      【解决方案2】:

      对于 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)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        相关资源
        最近更新 更多