You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS16锁屏播放按钮失效,iOS15(iPhone11)正常问题求助

问题排查与解决方案:iOS16锁屏播放/暂停按钮禁用问题

针对你使用AVPlayer+AVPlayerViewController播放网络视频时,iOS16锁屏控制按钮失效的问题,核心原因是iOS16对媒体会话管理和远程控制事件的处理逻辑做了更新,旧的beginReceivingRemoteControlEvents方式已不再可靠,以下是具体排查点和修复方案:

1. 必须配置后台音频模式

在Info.plist中添加UIBackgroundModes键,包含audio值,确保App在后台仍能维持音频会话:

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>

2. 更新AVAudioSession配置

iOS16推荐使用带mode和options的完整配置方法,明确指定媒体播放模式,避免会话状态异常:

NSError *sessionError = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
// 设置为电影播放模式,适配视频场景
BOOL success = [session setCategory:AVAudioSessionCategoryPlayback
                               mode:AVAudioSessionModeMoviePlayback
                            options:AVAudioSessionCategoryOptionMixWithOthers
                              error:&sessionError];
if (success) {
    [session setActive:YES error:nil];
} else {
    NSLog(@"Audio session setup failed: %@", sessionError.localizedDescription);
}

3. 替换旧的远程控制事件处理方式

弃用beginReceivingRemoteControlEvents,改用MPRemoteCommandCenter(iOS7+支持,iOS16下更稳定),主动注册播放/暂停命令:

#import <MediaPlayer/MediaPlayer.h>

// 在播放初始化时注册命令中心
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

// 播放命令
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
    if (self.player.rate == 0) {
        [self.player play];
        // 更新播放状态到系统
        [self updateNowPlayingInfo];
    }
    return MPRemoteCommandHandlerStatusSuccess;
}];

// 暂停命令
[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
    if (self.player.rate != 0) {
        [self.player pause];
        // 更新播放状态到系统
        [self updateNowPlayingInfo];
    }
    return MPRemoteCommandHandlerStatusSuccess;
}];

4. 维护MPNowPlayingInfoCenter状态

必须向系统同步当前播放状态,否则锁屏界面无法识别播放状态:

- (void)updateNowPlayingInfo {
    MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
    NSMutableDictionary *nowPlayingInfo = [[NSMutableDictionary alloc] init];
    
    // 必须设置播放状态
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = @(self.player.rate);
    // 可选:添加视频标题、时长等信息
    nowPlayingInfo[MPMediaItemPropertyTitle] = @"视频标题";
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = @(CMTimeGetSeconds(self.player.currentItem.duration));
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(CMTimeGetSeconds(self.player.currentItem.currentTime));
    
    infoCenter.nowPlayingInfo = nowPlayingInfo;
}

// 在player的rate变化时调用更新(比如KVO监听player.rate)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"rate"]) {
        [self updateNowPlayingInfo];
    }
}

5. 确保AVPlayerViewController的关联

如果使用AVPlayerViewController,需确保它的player属性正确赋值,且视图控制器已被正确呈现,iOS16对视图控制器的生命周期和媒体会话的绑定更严格。


内容的提问来源于stack exchange,提问作者Chayan Chakraborty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:43:07