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;
    }
}
相关推荐
the3clipse6 小时前
Windows 11 26H2 声音与音频问题排查指南:没声音、麦克风不收音与蓝牙音频优化
音视频·le audio·windows 11 26h2·win11没声音·麦克风没声音·蓝牙耳机音质·音频服务修复
开开心心就好11 小时前
PDF水印怎么去掉?勾选对象再点匹配
java·前端·mysql·支持向量机·pdf·音视频·模拟退火算法
ellis197012 小时前
Claude Code[二] 上下文相关的机制
unity·ai编程
RobinDevNotes1 天前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
伯奥克客服1 天前
2026年视频综合一体机架构选型:从接口协议到流媒体底层的深度技术解析
架构·音视频
开开心心就好1 天前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
zdr尽职尽责2 天前
Unity 3C 动画系统:从「蜘蛛网」到可维护的动画架构
unity·架构·游戏引擎
FansAI_art2 天前
多角色 AIGC 短片的双模型分工:静态生图与图生视频怎么接
aigc·音视频
国家一级摸鱼选手2 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
山顶夕景3 天前
【ECCV2026】流式视频理解Thinking in Streaming Video
音视频·vlm·视频理解·多模态理解·流式视频