[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()即可。

相关推荐
一种时光5 小时前
Unity 获取当前播放的动画,判断是否是某个动画
unity·游戏引擎
不绝1919 小时前
Unity入门 :场景叠加/预制体资源包/脚本资源/生命周期函数/Inspector页面
unity·游戏引擎
在路上看风景10 小时前
20. 资源和脚本的绑定关系
unity
yj爆裂鼓手13 小时前
unity对象池
unity·c#
在路上看风景13 小时前
3.7 SRP Batcher
unity
快乐觉主吖13 小时前
Unity方便修改产品名和包名的小工具
unity·游戏引擎
JIes__1 天前
Unity(二)——MonoBehavior中的重要内容
unity·游戏引擎
4Forsee2 天前
【增强现实】快速上手 Vuforia Unity Android AR 应用开发
android·unity·ar
两水先木示2 天前
【Unity】对指定物体进行描边——模板测试法
unity·游戏引擎·shader·外描边
Miss_SQ2 天前
实现Unity录音、百度云语音转文字
unity·语音识别