MPMoviePlayerController - 检测按下下一个/上一个按钮

11

我正在使用 MPMoviePlayerController,我需要检测用户按下“下一个/上一个”按钮。我已尝试了多种方法,但都没有奏效。

以下是我尝试过的内容:

  • 遥控器事件
-(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方法从未被调用。我读过,这在iOS版本高于6的情况下不起作用 - 我正在使用iOS 7。

  • 通知

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

有什么想法吗?


https://dev59.com/-lDTa4cB1Zd3GeqPGRuQ - Woodstock
正如您在我的帖子中所看到的,那是我尝试的第一件事,但它并没有起作用。 - Jan Podgorski
当MPMoviePlayerController显示时,是否会调用viewWillDisappear并结束接收远程控制?请尝试注释掉viewWillDisappear并再次尝试。 - John
@John 谢谢你的建议,我也这么想,但没这么幸运!:( - taber
你看过 MPMoviePlayerNowPlayingMovieDidChangeNotification 吗?它会告诉你当前正在播放的电影何时更改,如果你有一个包含了电影信息的字典或数组,你就可以确定正在播放的电影是下一部还是上一部。https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/#//apple_ref/c/data/MPMoviePlayerNowPlayingMovieDidChangeNotification - Emil
@Emil 是的,我试过了,但是在点击上一个或下一个后通知没有触发。 - taber
5个回答

0

您需要注册以处理与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");
    }
}

0

尝试在以下事件中注册:

- (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并实现-(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent,你试过了吗? - hariszaman

0

抱歉,我不太理解你的问题,但如果你想在控制中心使用应用程序之外的控件,你可以使用以下方法:

      // 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)];

0

你尝试过使用MPMoviePlayerNowPlayingMovieDidChangeNotification吗? 如果这不起作用,我建议您转向更低级别的API,即AVPlayer。它可以在视频播放和其他情况下提供对所有操作的精细控制。


0

您可以像更改UIImagePickerController的overlayView一样更改MPMoviePlayerController的overlayView,以实现所需的功能。

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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接