gpt4 book ai didi

iphone - AVPlayerLayer 进入后台模式时停止播放

转载 作者:行者123 更新时间:2023-12-03 21:17:15 25 4
gpt4 key购买 nike

我使用以下命令设置 AVPlayerLayer:

        audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:fileName]];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:audioPlayer];
[avPlayerLayer setFrame:self.view.bounds];
[[self.view layer] addSublayer:avPlayerLayer];
[audioPlayer play];

在 AppDelegete 中我添加:

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

现在在 UIViewController 类中我添加:

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

现在的问题是,当我进入后台模式时,歌曲停止播放,但是当我按下 Remote 上的播放按钮(双击主页按钮时弹出的菜单)时,歌曲继续从哪里播放它停了。

知道为什么我会遇到这个问题吗?

最佳答案

查看此解决方案:

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

关键在信息 plist 中的 UIBackgroundModes 中。

关于iphone - AVPlayerLayer 进入后台模式时停止播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com