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

相关推荐
ellis197010 小时前
LuaC API知识点汇总
unity·lua
charlie11451419117 小时前
理解C++20的革命特性——协程引用之——利用协程做一个迷你的Echo Server
网络·学习·socket·c++20·协程·epoll·raii
charlie1145141911 天前
理解C++20的革命特性——协程支持2:编写简单的协程调度器
c++·学习·算法·设计模式·c++20·协程·调度器
雪下的新火2 天前
爆炸特效-Unity-04-shader&粒子系统
经验分享·笔记·unity·游戏引擎·shader·粒子系统
闲人编程2 天前
使用Celery处理Python Web应用中的异步任务
开发语言·前端·python·web·异步·celery
大Mod_abfun2 天前
Unity游戏基础-2(初识场景~项目构建)
游戏·unity·游戏引擎
charlie1145141913 天前
理解C++20的革命特性——协程支持1
c++·学习·c++20·协程·语言特性·调度·现代c++
爱吃小胖橘3 天前
Lua语法
开发语言·unity·lua
qq_205279053 天前
unity 读取PPT显示到屏幕功能
unity·游戏引擎·powerpoint
tiankongdeyige3 天前
Unity学习之C#的反射机制
学习·unity·c#