【小功能】Unity音频截取部分内容播放

Unity音频截取部分播放

今天接到一个需求从Json文件里可以解析出音频起始时间和结束时间,需要根据此信息只播放某个音频文件起始时间——结束时间的音效(相当于片段)。
搜了下发现信息不是很多,而且感觉很多写的都有问题(头大,还有没注释也就算了,还是错的,啊啊啊~),后续根据网上看到的部分API按思路实现了下,代码如下:

    /// <summary>
    /// 截取音频片段音效并播放
    /// </summary>
    /// <param name="audioClip">截取的音频</param>
    /// <param name="startTime">截取的开始时间</param>
    /// <param name="endTime">截取的结束时间</param>
    public void PlayAudioClipSegment(AudioClip audioClip, float startTime, float endTime)
    {
        // 计算截取片段的长度
        float clipLength = endTime - startTime;

        // 创建一个新的截取剪辑
        AudioClip segment = AudioClip.Create("TempAudio", Mathf.RoundToInt(clipLength * audioClip.frequency), audioClip.channels, audioClip.frequency, false);

        // 计算截取的样本数
        int startSample = Mathf.RoundToInt(startTime * audioClip.frequency);
        int endSample = Mathf.RoundToInt(endTime * audioClip.frequency);
        int segmentLength = endSample - startSample;

        // 从原始音频剪辑中复制样本到截取剪辑
        float[] data = new float[segmentLength * audioClip.channels];
        audioClip.GetData(data, startSample);

        //数据设置进新的片段里
        segment.SetData(data, 0);

        // 播放截取的音频片段
        audioSource.clip = segment;
        audioSource.Play();
    }

目前发现这种方法可行,但不清楚有没有更好或更便捷的方法,我尝试过
audioSource.time = startTime; audioSource.SetScheduledEndTime等来实现,网上也看到有这样写的,但我测试出来并没有得到我想要的效果(不知道是不可行还是我写的不对)。所以目前暂用这种方法,如果有小伙伴有其他方法,跪求大佬告知。

DisUnity v0.2.1 提取.assets .unity3d 文本 音频 字体 纹理图片 shader脚本等 不可用于非法破解他人资源,不可用于商业目的,仅供学习研究为目的!任何责任与上传者无关! 需JVM!!! 帮助: I've been working on an asset extraction tool for Unity-based games, since I needed such a tool for two Unity games I have. Moreover, the game engine has become very popular, so the unpacker should theoretically work with thousands of different games. Right now, it supports text, audio clips, most textures (including movie textures), fonts and substances from Unity versions ranging from 2.6 to 4.2. Support for static meshes is also planned, but will be added later, due to the complexity of the mesh data. There's no GUI for the tool yet, since it's still in an early experimental stage. So if you want to use it, you should have at least basic knowledge with the command line interface. :wink: Both source code and compiled builds are available on GitHub here. It is written in Java, so you'll also need to install the Java VM. For a quick start, you can use DisUnity like this: Code: disunity "resources.assets" (also works with .unity3d files) which is equivalent to: Code: disunity -c extract "resources.assets" If that doesn't work, try this: Code: java -jar disunity.jar "resources.assets" The files should then appear in a subfolder with the same name as the file. You can also try to play with the other parameters and commands that are listed with the -h parameter, but I think most normal people won't need these right now.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值