AVPlayer在后台播放视频

时间:2012-05-07 07:59:27

标签: iphone objective-c

我正在使用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打印到控制台

我需要添加别的东西吗?

1 个答案:

答案 0 :(得分:1)

只需添加[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];和其他一些调整。它全是here

相关问题