Health/Assets/_VoiceAssistant/AIChatTookit/Scripts/Chat/VoiceInputs.cs

59 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VoiceInputs : MonoBehaviour
{
/// <summary>
/// 录制的音频长度
/// </summary>
public int m_RecordingLength = 5;
public AudioClip recording;
public bool IsRecording { get; private set; }
public event Action<AudioClip> onRecordOver;
/// <summary>
/// 开始录制声音
/// </summary>
public void StartRecordAudio()
{
IsRecording = true;
recording = Microphone.Start(null, false, m_RecordingLength, 16000);
Debug.Log("开始录音...");
StartCoroutine(StopRecordAsync());
}
IEnumerator StopRecordAsync()
{
yield return new WaitForSeconds(m_RecordingLength);
StopRecordAudio();
}
/// <summary>
/// 结束录制返回audioClip
/// </summary>
/// <param name="_callback"></param>
public void StopRecordAudio()
{
#if UNITY_WEBGL && !UNITY_EDITOR
signalManager.onAudioClipDone += _callback;
signalManager.StopRecordBinding();
#else
Microphone.End(null);
if(onRecordOver != null)
onRecordOver(recording);
IsRecording = false;
Debug.Log("停止录音");
#endif
}
}