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

相关推荐
程序猿多布18 小时前
XLua教程之热补丁技术
unity·c#·lua·xlua
SmalBox21 小时前
【光照】Unity中的[光照模型]概念辨析
unity·渲染
挂科是不可能出现的21 小时前
unity导入blender动画
unity·游戏引擎·blender
派葛穆1 天前
Unity-按钮实现场景跳转
java·unity·游戏引擎
程序猿多布1 天前
XLua教程之Lua调用C#
unity·c#·lua·xlua
lrh30251 天前
Custom SRP - Point and Spot Lights
unity·srp·render pipeline
绀目澄清1 天前
unity UGUI 鼠标画线
unity·计算机外设·游戏引擎
作孽就得先起床2 天前
unity pcd 二进制版 简单显示文件对象(单色)
unity·游戏引擎
SmalBox2 天前
【光照】Unity[经验模型]和[物理模型]
unity·渲染
在路上看风景2 天前
10. 游戏开发中的TCP与UDP
unity