Unity之音频剪辑提问,流式语音回复使用示例

引用

Unity之语音提问,语音答复

Unity之使用火山引擎实现音频剪辑提问,流式语音回复

示例

通过RecordVoice组件录制音频,

将录制音频作为参数传入VoiceQuestion组件中,播放音频

csharp 复制代码
using UnityEngine;

public class UseVoiceQuestion : MonoBehaviour
{
    [SerializeField] RecordVoice recordVoice;
    [SerializeField] VoiceQuestion voiceQuestion;

    KeyCode voiceInputKey = KeyCode.Space;
    bool enableVoiceInput = true;
    bool isRecordIng = false;

    void Awake()
    {
        recordVoice.OnRecordAudioCompleted += OnRecordVoiceCompleted;
        voiceQuestion.OnTextAnswerCompleted += OnTextAnswerCompleted;
        voiceQuestion.OnError += OnVoiceQuestionError;
    }

    void OnRecordVoiceCompleted(AudioClip audioClip)
    {
        voiceQuestion.InputQuestionAudio(audioClip);
    }

    void OnTextAnswerCompleted()
    {
        enableVoiceInput = true;
    }

    void OnVoiceQuestionError()
    {
        enableVoiceInput = true;
    }

    void Update()
    {
        if (enableVoiceInput == false) return;

        if (Input.GetKeyDown(voiceInputKey))
        {
            if (isRecordIng == false)
            {
                if (recordVoice.ExistMicrophoneDevice())
                {
                    Debug.Log("录音开始");
                    isRecordIng = true;
                    recordVoice.BeginRecord();
                }
                else
                {
                    Debug.LogError("没有录音设备,无法录音");
                }
            }
        }

        if (Input.GetKeyUp(voiceInputKey))
        {
            if (isRecordIng)
            {
                Debug.Log("录音结束");
                isRecordIng = false;
                enableVoiceInput = false;
                recordVoice.EndRecord();
            }
        }
    }

    void OnGUI()
    {
        GUI.skin.label.fontSize = 40;
        if (enableVoiceInput)
        {
            if (isRecordIng == false)
                GUILayout.Label("按下空格键,进行语音输入");
            else
                GUILayout.Label("语音录制中");
        }
        else
        {
            GUILayout.Label("等待提问结束");
        }
    }

    void OnDestroy()
    {
        voiceQuestion.OnError -= OnVoiceQuestionError;
        voiceQuestion.OnTextAnswerCompleted -= OnTextAnswerCompleted;
        recordVoice.OnRecordAudioCompleted -= OnRecordVoiceCompleted;
    }
}
相关推荐
可乐鸡翅yeah_9 小时前
FFmpeg 生成 HLS 独立分片 independent‑segments 参数到底有什么用
javascript·ffmpeg·音视频·safari·m3u8
企业数字化笔记11 小时前
视频理解怎样从“看见”变成“判断”?几何规则、轨迹特征与动作模型的选型
音视频
小猴子爱上树18 小时前
跨马翻译:批量图片翻译+视频字幕+智能抠图,跨境电商在线图片翻译工具
大数据·人工智能·python·音视频
阿明副业观察20 小时前
AI视频创作流程怎么做?完整指南与工具推荐
大数据·人工智能·aigc·音视频·ai写作
aichitang202420 小时前
应用数学与音频信号处理
音视频·信号处理
可乐鸡翅yeah_1 天前
hls.js 缓冲区参数 maxBufferLength、maxBufferSize 通俗讲解与业务调参
开发语言·javascript·ios·音视频·safari·m3u8
小宋10211 天前
视频 RAG 从零实现:镜头切分、字幕对齐与时间段引用
人工智能·音视频
Dovis(誓平步青云)1 天前
导览音频切换太快,旧讲解不能覆盖新展品
android·前端·javascript·ecmascript·音视频·宠物
归秋1422 天前
多轨音频智能混音软件怎么选:从 demo 到更完整作品的 AI 后期工具思路
人工智能·音视频
小猴子爱上树2 天前
跨马翻译:批量图片翻译与视频字幕、智能抠图一站式在线图片翻译工具
大数据·人工智能·python·音视频