59 lines
1.3 KiB
C#
59 lines
1.3 KiB
C#
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
|
||
|
||
}
|
||
|
||
}
|