[Unity]播放音频卡顿问题

记录一个问题:

游戏内播放完音频A再去循环播放音频B,在协程里使用等待n秒来实现拼接,发现在个别手机上会有卡顿的问题,盲猜是和帧率有关。

这是最初的实现方案:

cs 复制代码
    IEnumerator IEPlayAudio()
    {
        if(ASOnBeginDrag != null)
        {
            AudioClip clip = ASOnBeginDrag.clip;
            if(clip != null)
            {
                float m_AudioLen = clip.length;
                ASOnBeginDrag.Play();
                yield return new WaitForSeconds(m_AudioLen);
                if (ASOnDrag != null)
                {
                    ASOnDrag.Play();
                    ASOnDrag.loop = true;
                }
            }
        }
        yield return null;
    }

这是修改后的代码:

cs 复制代码
    void PlayBeginDragAudio()
    {
        if(ASOnBeginDrag != null)
        {
            ASOnBeginDrag.Play();
            if (ASOnDrag != null)
            {
                //使用Audio System绝对时间来处理音频拼接问题
                ASOnDrag.PlayScheduled(AudioSettings.dspTime + m_AudioLen);
                ASOnDrag.loop = true;
            }
        }
    }

AudioSourec.PlayScheduled(AudioSettings.dspTime + timeInterval);

表示在当前Audio System绝对时间之后的timeInterval秒,来播放;同时想要停止这个操作只需要AudioSourec.Stop()即可。

相关推荐
我的offer在哪里18 小时前
示例 Unity 项目结构(Playable Game Template)
unity·游戏引擎
淡海水20 小时前
【节点】[Branch节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·branch
在路上看风景21 小时前
4.6 显存和缓存
unity
Zik----1 天前
简单的Unity漫游场景搭建
unity·游戏引擎
源文雨1 天前
shell调用ffmpeg递归转换所有wav至flac的脚本
ffmpeg·bash·音视频·音频·unix·shell·音频编码
在路上看风景1 天前
4.5 顶点和片元
unity
仙剑魔尊重楼2 天前
音乐制作电子软件FL Studio2025.2.4.5242中文版新功能介绍
windows·音频·录屏·音乐·fl studio
在路上看风景2 天前
31. Unity 异步加载的底层细节
unity
天人合一peng2 天前
Unity中做表头时像work中整个调整宽窄
unity
小李也疯狂2 天前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap