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

iOS中AVAudioPlayer锁屏返回应用后播放失败问题求助

问题解决:锁屏解锁后AVAudioPlayer播放失败的错误排查与修复

一、获取播放失败的错误信息

1. 捕获音频会话设置错误

改用带NSError参数的重载方法,捕获会话初始化或激活阶段的错误:

NSError sessionError = null;
var success = AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback, out sessionError);
if (!success && sessionError != null)
{
    Console.WriteLine($"会话设置错误:{sessionError.LocalizedDescription}");
}

success = AVAudioSession.SharedInstance().SetActive(true, out sessionError);
if (!success && sessionError != null)
{
    Console.WriteLine($"会话激活错误:{sessionError.LocalizedDescription}");
}

2. 监听音频会话中断通知

订阅中断相关通知,获取中断原因及恢复时的状态细节:

// 初始化阶段注册通知
NSNotificationCenter.DefaultCenter.AddObserver(AVAudioSession.InterruptedNotification, HandleInterruption);
NSNotificationCenter.DefaultCenter.AddObserver(AVAudioSession.InterruptionEndedNotification, HandleInterruptionEnded);

// 中断开始处理
void HandleInterruption(NSNotification notification)
{
    var info = notification.UserInfo;
    var interruptionType = (AVAudioSessionInterruptionType)info[AVAudioSession.InterruptionTypeKey].Int32Value;
    Console.WriteLine($"音频会话中断类型:{interruptionType}");
}

// 中断结束处理
void HandleInterruptionEnded(NSNotification notification)
{
    var info = notification.UserInfo;
    var recoveryOptions = (AVAudioSessionInterruptionOptions)info[AVAudioSession.InterruptionOptionKey].UInt32Value;
    Console.WriteLine($"中断恢复选项:{recoveryOptions}");
}

3. 检查AVAudioPlayer准备状态错误

在Play()返回false时,重新尝试准备并捕获错误:

if (!audioPlayer.Play())
{
    NSError prepareError = null;
    bool prepareSuccess = audioPlayer.PrepareToPlay(out prepareError);
    if (!prepareSuccess && prepareError != null)
    {
        Console.WriteLine($"播放器准备失败:{prepareError.LocalizedDescription}");
    }
    // 再次尝试播放
    audioPlayer.Play();
}

二、修复方案

1. 中断结束后轻量恢复播放器

在中断结束通知中,重新激活会话并为播放器执行准备操作,无需重建实例:

void HandleInterruptionEnded(NSNotification notification)
{
    NSError error = null;
    // 重新激活音频会话
    AVAudioSession.SharedInstance().SetActive(true, out error);
    if (error != null)
    {
        Console.WriteLine($"重新激活会话失败:{error.LocalizedDescription}");
        return;
    }

    // 遍历单例中的播放器,重新准备失效的实例
    foreach (var player in YourSingleton.AudioPlayers)
    {
        if (!player.PreparedToPlay)
        {
            player.PrepareToPlay();
        }
    }
}

2. 按需重建单个播放器

无需一次性重载所有88个音效,仅在特定播放器播放失败时重建该实例:

if (!audioPlayer.Play())
{
    var audioUrl = audioPlayer.Url;
    // 释放旧实例
    audioPlayer.Dispose();
    // 重建播放器
    audioPlayer = new AVAudioPlayer(audioUrl, null, out var createError);
    if (createError != null)
    {
        Console.WriteLine($"重建播放器失败:{createError.LocalizedDescription}");
        return;
    }
    audioPlayer.PrepareToPlay();
    audioPlayer.Play();
    // 更新单例中的对应播放器引用
    YourSingleton.UpdateAudioPlayer(audioPlayer);
}

3. 前台激活时批量检查状态

在应用回到前台时,批量检查播放器准备状态,仅重新准备失效的实例:

NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, (notification) =>
{
    foreach (var player in YourSingleton.AudioPlayers)
    {
        if (!player.PreparedToPlay)
        {
            player.PrepareToPlay();
        }
    }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 10:02:27