2

AVPlayerを使用してアプリでビデオを再生していますが、ビデオをバックグラウンドモードで再生させたいと考えています。

これは私が入れたもの- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsです:

[[AVAudioSession sharedInstance] setDelegate: self];    
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

そして私もplistに追加します:必要なバックグラウンドモード->アプリはオーディオを再生します

私もこれを追加します:

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

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            NSLog(@"4");
            break;
        case UIEventSubtypeRemoteControlPlay:
            break;
        case UIEventSubtypeRemoteControlPause:
            NSLog(@"3");
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            NSLog(@"2");
            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            NSLog(@"1");
            break;
        default:
            break;
    }
}

そして、アプリをバックグラウンドに移動してボタンを押すと、nslogがコンソールに出力します

他に何か追加する必要がありましたか?

4

1 に答える 1

1

と他のいくつかの微調整を追加するだけ[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];です。それはすべてここにあります。

于 2012-05-07T08:07:14.320 に答える