MPMoviePlayerController不适用于remoteControlReceivedWithEvent

时间:2012-08-08 02:19:37

标签: objective-c ios mpmovieplayercontroller

我想允许键盘上的控件在我的应用中运行。控件使用Apple的远程控制事件(beginReceivingRemoteControlEventsendReceivingRemoteControlEventsremoteControlReceivedWithEvent);但我似乎无法使用MPMoviePlayerController

即使在开始时调用了beginReceivingRemoteControlEvents,我也没有在程序开始时看到任何事件。 在播放视频时我没有看到任何事件。 关闭视频后,我确实看到了事件。

从上面看,似乎MPMoviePlayerController的音频流禁用了控件。但是我不知道如何改变这一点。我尝试使用[moviePlayer setUseApplicationAudioSession:NO];更改音频以使用系统会话,但它什么也没做。

这是我的设置。我的应用代表是UIViewController。我将主窗口的根视图控制器设置为app delegate,将视图添加到视图控制器,并在视图控制器中添加与视频有关的部分。

- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)theIndexPath {
    NSString *file = [[MGMFilesPath stringByExpandingTildeInPath] stringByAppendingPathComponent:[files objectAtIndex:[theIndexPath indexAtPosition:1]]];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    [audioSession setActive:YES error:nil];
    NSLog(@"%d", [self isFirstResponder]);

    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:file]];

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

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
        [[self view] addSubview:[moviePlayer view]];
        [moviePlayer setFullscreen:YES animated:YES];
        [moviePlayer play];
    } else {
        [moviePlayer play];
    }
    [fileView deselectRowAtIndexPath:theIndexPath animated:NO];
}

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

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

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    NSLog(@"remoteControlReceivedWithEvent: %@", event);
    if (event.type==UIEventTypeRemoteControl) {
        if (event.subtype==UIEventSubtypeRemoteControlPlay) {
            NSLog(@"Play");
        } else if (event.subtype==UIEventSubtypeRemoteControlPause) {
            NSLog(@"Pause");
        } else if (event.subtype==UIEventSubtypeRemoteControlTogglePlayPause) {
            NSLog(@"Play Pause");
        }
    }
}

- (void)exitedFullscreen:(NSNotification*)notification {
    [[moviePlayer view] removeFromSuperview];
    [moviePlayer stop];
    [moviePlayer release];
    moviePlayer = nil;
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)playbackFinished:(NSNotification*)theNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    NSNumber *reason = [[theNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    if ([reason intValue]!=MPMovieFinishReasonUserExited) {
        [moviePlayer setFullscreen:NO animated:YES];
        [[moviePlayer view] removeFromSuperview];
        [moviePlayer stop];
        [moviePlayer release];
        moviePlayer = nil;
        [[AVAudioSession sharedInstance] setActive:NO error:nil];
    }
    NSLog(@"%d", [self isFirstResponder]);
}

正如您在上面的代码中所看到的那样,我确认它是第一响应者而且确实如此,所以我知道这不是第一响应者问题。

有人可以帮我解决这个问题吗?

由于

1 个答案:

答案 0 :(得分:1)

显然,MPMoviePlayerController不是这样做的。我最终做的是使用 MPMoviePlayerViewController 覆盖 remoteControlReceivedWithEvent 来自定义控件。以下是我目前正在使用的代码。

@interface MGMMoviePlayerViewController : MPMoviePlayerViewController
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
@end

@implementation MGMMoviePlayerViewController
- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent {
    if (theEvent.type==UIEventTypeRemoteControl) {
        if (theEvent.subtype==UIEventSubtypeRemoteControlPlay) {
            [[self moviePlayer] play];
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlPause) {
            [[self moviePlayer] pause];
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlTogglePlayPause) {
            if ([[self moviePlayer] playbackState]==MPMoviePlaybackStatePlaying) {
                [[self moviePlayer] pause];
            } else {
                [[self moviePlayer] play];
            }
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlStop) {
            [[self moviePlayer] stop];
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlNextTrack) {
            NSTimeInterval currentTime = [[self moviePlayer] currentPlaybackTime];
            currentTime += 10;
            if (currentTime>[[self moviePlayer] duration])
                currentTime = [[self moviePlayer] duration];
            [[self moviePlayer] setCurrentPlaybackTime:currentTime];
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlPreviousTrack) {
            NSTimeInterval currentTime = [[self moviePlayer] currentPlaybackTime];
            currentTime -= 10;
            if (currentTime<0)
                currentTime = 0;
            [[self moviePlayer] setCurrentPlaybackTime:currentTime];
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlBeginSeekingBackward) {
            [[self moviePlayer] beginSeekingBackward];
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlBeginSeekingForward) {
            [[self moviePlayer] beginSeekingForward];
        } else if (theEvent.subtype==UIEventSubtypeRemoteControlEndSeekingBackward || theEvent.subtype==UIEventSubtypeRemoteControlEndSeekingForward) {
            [[self moviePlayer] endSeeking];
        }
    }
}
@end


- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)theIndexPath {
    NSString *file = [[MGMFilesPath stringByExpandingTildeInPath] stringByAppendingPathComponent:[files objectAtIndex:[theIndexPath indexAtPosition:1]]];

    moviePlayerView = [[MGMMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:file]];

    [self presentMoviePlayerViewControllerAnimated:moviePlayerView];
    [[moviePlayerView moviePlayer] play];

    [fileView deselectRowAtIndexPath:theIndexPath animated:NO];
}