0

我想让键盘上的控件在我的应用程序中工作。控件使用 Apple 的远程控制事件(beginReceivingRemoteControlEventsendReceivingRemoteControlEventsremoteControlReceivedWithEvent);但是我似乎无法使用它MPMoviePlayerController

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

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

这是我的设置。我的应用程序委托是一个UIViewController. 我将主窗口的根视图控制器设置为应用程序委托,将视图添加到视图控制器并在视图控制器中添加与视频有关的部分。

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

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

有人可以帮我完成这项工作吗?

谢谢

4

1 回答 1

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];
}
于 2012-08-11T15:38:52.923 回答