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

文章介绍了如何在Unity中根据Json文件解析的起始和结束时间播放音频片段。通过创建新的AudioClip,复制原始音频的指定样本并播放,实现了音频的截取播放功能。作者提到尝试过使用audioSource.time和audioSource.SetScheduledEndTime等方法,但未达到预期效果,目前的方法是可行的,但寻求可能的优化方案。
摘要由CSDN通过智能技术生成

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.
Unity 科大讯飞语音听写是一种基于科大讯飞语音听写 API 的语音识别功能。然而,由于语音识别 API 限制了音频数据的长度,长音频可能会超过 API 的限制,因此在使用时我们需要将音频数据进行分割处理。 分割音频数据的目的是将长音频切分成多个较短的音频片段,每个片段的长度都在 API 的限制范围内。我们可以采用以下步骤来进行音频数据的分割处理: 1. 首先,对于要进行语音识别的长音频,在 Unity 中进行音频录制或导入,得到音频数据。 2. 接下来,根据科大讯飞语音听写 API 的限制,确定每个音频片段的最大长度。通常,最大长度为60秒或者更少。 3. 然后,使用编程语言或者相应的库,对音频数据进行分割。可以根据每个音频片段的最大长度,在音频数据中截取对应的片段。 4. 分割后的音频片段可以进行批量的语音识别操作。每个片段会被分别发送给科大讯飞语音听写 API 进行识别,返回相应的识别结果。 需要注意的是,在分割音频数据时,应确保片段之间的分割点位置合适,以免影响语音识别的准确性。另外,对于长篇的语音内容,将其分割成多个片段也有助于提高识别的实时性和可靠性。 综上所述,通过对音频数据进行分割处理,可以保证长音频在使用科大讯飞语音听写 API 进行语音识别时不会超过其限制。这样可以有效利用 API 的功能,实现准确、高效的语音识别应用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值