MPMoviePlayerController - 检测按下 Next/Prev 按钮

Posted

技术标签:

【中文标题】MPMoviePlayerController - 检测按下 Next/Prev 按钮【英文标题】:MPMoviePlayerController - detect pressing Next/Prev buttons 【发布时间】:2014-08-19 11:06:19 【问题描述】:

我正在使用MPMoviePlayerController,我需要检测按下 Next/Prev 按钮。我尝试了几件事,但似乎都不起作用。

这是我尝试过的:

远程控制事件
-(void) viewWillAppear:(BOOL)animated

    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
  
-(void) viewWillDisappear:(BOOL)animated

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
  
-(BOOL)canBecomeFirstResponder

    return YES;
  
-(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent

    // stuff

问题是remoteControlReceivedWithEvent 方法永远不会被调用。我已经读到这在高于 6 的 ios 版本中不起作用 - 我正在使用 iOS 7

通知

我尝试使用 MPMoviePlayerPlaybackStateDidChangeNotification 并检查 MPMoviePlaybackStateSeekingForwardMPMoviePlaybackStateSeekingBackward - 不幸的是,这些播放状态是在拖动播放栏时设置的,而不是在按下 Next/Prev 按钮时设置的。

有什么想法吗?

【问题讨论】:

***.com/questions/3593683/… 正如您在我的帖子中看到的那样,这是我尝试的第一件事,但它不起作用 会不会是MPMoviePlayerController显示的时候调用了viewWillDisappear所以接收遥控结束了?请尝试将 viewWillDisappear 注释掉,然后重试。 @John 感谢您的建议,我也有同样的想法,没有这样的运气! :( 你看过 MPMoviePlayerNowPlayingMovieDidChangeNotification 了吗?它会告诉您当前播放的电影何时发生变化,如果您有包含电影的字典或数组,您可以确定正在播放的电影是下一个还是上一个。 developer.apple.com/library/ios/documentation/MediaPlayer/… 【参考方案1】:

对不起,我不太了解您的问题,但是如果您想在控制中心使用您的应用程序中的控件,您可以使用:

      // You need cath the singleton
MPRemoteCommandCenter *myRemote = [MPRemoteCommandCenter sharedCommandCenter];
//And add the selector you can fire depends on the button, a couple of examples:
[myRemote.playCommand addTarget:self action:@selector(myPlayMethods)];
[myRemote.nextTrackCommand addTarget:self action:@selector(myNextTrackMethods)];

【讨论】:

【参考方案2】:

尝试注册以下活动:

- (void)viewDidAppear:(BOOL)animated 
    [super viewDidAppear:animated];

    // Turn on remote control event delivery
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    // Set itself as the first responder
    [self becomeFirstResponder];

也不要设置kAudioSessionProperty_OverrideCategoryMixWithOthers属性

【讨论】:

也有解决方案说子类 Uiapplication 和 impelment -(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent 你试过了吗?【参考方案3】:

您是否尝试过 MPMoviePlayerNowPlayingMovieDidChangeNotification? 如果这不起作用,那么我建议转移到较低级别的 API,即 AVPlayer。它在视频播放和其他情况下提供对所有操作的细粒度控制。

【讨论】:

【参考方案4】:

您需要注册才能处理moviePlayerLoadStateChanged 的​​通知。当您按下下一个/上一个按钮时,moviePlayerLoadStateChanged 将被调用,并且 loadState 将为 MPMovieLoadStateUnknown

-(void)registerMyStuff 
    [[NSNotificationCenter defaultCenter] addObserver:self
                            selector:@selector(moviePlayerLoadStateChanged:)
                                    name:MPMoviePlayerLoadStateDidChangeNotification
                                           object:self.mpc];


- (void)moviePlayerLoadStateChanged:(NSNotification *)notification

    MPMoviePlayerController *moviePlayer = notification.object;
    MPMovieLoadState loadState = moviePlayer.loadState;

    if(loadState == MPMovieLoadStateUnknown)
    
        // this is where the next/prev buttons notify
        // there is no video in this state so load one
        // just reload the default movie

        NSLog(@"MPMovieLoadStateUnknown");
        self.mpc.contentURL = self.fileURL;
        [self.mpc prepareToPlay];

        return;
    
    else if(loadState & MPMovieLoadStatePlayable)
    
        NSLog(@"MPMovieLoadStatePlayable");
    
    else if(loadState & MPMovieLoadStatePlaythroughOK)
    
        NSLog(@"MPMovieLoadStatePlaythroughOK");
     else if(loadState & MPMovieLoadStateStalled)
    
        NSLog(@"MPMovieLoadStateStalled");
    

【讨论】:

【参考方案5】:

你改变MPMoviePlayerControlleroverlayView就像改变UIImagePickerControlleroverlayView来实现你需要的功能。

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
    initWithContentURL:someUrl];

moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];

NSArray *windows = [[UIApplication sharedApplication] windows];

if ([windows count] > 1) 

     UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
     [moviePlayerWindow addSubview:yourCustomOverlayView];

【讨论】:

以上是关于MPMoviePlayerController - 检测按下 Next/Prev 按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何显示 MPMoviePlayerController 控件?

MPMoviePlayerController 隐藏 AirPlay 按钮

MPMoviePlayerController 上的专辑封面

MPMoviePlayerController 已弃用,现在怎么办?

用 MPMoviePlayerController 替换 AVPlayer

“MPMoviePlayerController”的“initialPlaybackTime”不起作用