首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayerLayer在进入后台模式时停止播放

AVPlayerLayer在进入后台模式时停止播放
EN

Stack Overflow用户
提问于 2012-05-07 23:11:39
回答 1查看 2.2K关注 0票数 3

我用以下命令设置AVPlayerLayer:

代码语言:javascript
复制
        audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:fileName]];
        avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:audioPlayer];
        [avPlayerLayer setFrame:self.view.bounds];
        [[self.view layer] addSublayer:avPlayerLayer];
        [audioPlayer play];

在AppDelegete中,我添加了:

代码语言:javascript
复制
[[AVAudioSession sharedInstance] setDelegate:self];    
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

现在,在UIViewController类中,我添加了:

代码语言:javascript
复制
-(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:
            if (audioPlayer.rate == 0.0) {
                [avPlayerLayer.player play];
            } else {
                [avPlayerLayer.player pause];
            }
            NSLog(@"4");
            break;
        case UIEventSubtypeRemoteControlPlay:
            [avPlayerLayer.player play];
            break;
        case UIEventSubtypeRemoteControlPause:
            [avPlayerLayer.player pause];
            NSLog(@"3");
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            [self didPressNextSong:nil];
            NSLog(@"2");
            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            [self didPressLastSong:nil];
            NSLog(@"1");
            break;
        default:
            break;
    }
}

现在的问题是,当我移动到背景模式时,歌曲停止播放,但当我按遥控器上的播放按钮(双击home按钮时弹出的菜单),歌曲继续从它停止的地方播放。

你知道为什么我会有这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-07 23:36:19

请参阅此解决方案:

How do I get my AVPlayer to play while app is in background?

密钥在info plist的UIBackgroundModes中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10484536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档