1

MPMoviePlayer在我的视图控制器中使用。在viewwillappear我正在做

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

但是-(void)remoteControlReceivedWithEvent:(UIEvent *)event当我单击播放器上的下一首曲目或上一首曲目按钮时,不会随时被调用。

我还包括以下代码viewwillappear

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];

让我知道可能是什么问题?或者给我一个示例或一些示例代码,我们可以在其中通知UIEventSubtypeRemoteControlNextTrackUIEventSubtypeRemoteControlPreviousTrack事件。

4

1 回答 1

-1

在我的 appdelegate 我有..

NSError *myErr;
NSError *activationError;
AVAudioSession *session = [AVAudioSession sharedInstance];

if( [session setCategory:AVAudioSessionCategoryPlayback error:&myErr] ) {
    if( [UIApplication instancesRespondToSelector: @selector (beginReceivingRemoteControlEvents)] ) {
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    }

    [session setDelegate: self];

    if( ![session setActive: YES error: &activationError] ) {
        NSLog(@"Audio Session error %@, %@- unable to setActive", activationError, [activationError userInfo]);
        return false;
    }
}
else    {
    NSLog(@"Audio Session error %@, %@ - unable to setCategory", myErr, [myErr userInfo]);
    return false;
}

然后在我的 ViewController 我有..

- (BOOL) canBecomeFirstResponder {
    return YES;
}

- (void) viewDidAppear:(BOOL)animated       {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlPlay:
                break;

            case UIEventSubtypeRemoteControlPause:
                break;

            case UIEventSubtypeRemoteControlStop:
                break;

            case UIEventSubtypeRemoteControlTogglePlayPause:
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                break;

            case UIEventSubtypeRemoteControlBeginSeekingBackward:
                break;

            case UIEventSubtypeRemoteControlEndSeekingBackward:
                break;

            case UIEventSubtypeRemoteControlBeginSeekingForward:
                break;

            case UIEventSubtypeRemoteControlEndSeekingForward:
                break;

            default:
                break;
        }
    }
}

希望这可以帮助。

于 2012-07-05T11:54:24.270 回答