其他操作就不在一一贅述了,直說一下添加的負責錄音的那段代碼
1、首先利用Microphone類開始錄製和結束錄製音頻
2、利用AudioSource類播放錄製的音頻
3、然後用自己寫的方法Float2Byte將錄製的音頻轉成byte數據,供訊飛語音識別方法調用
詳細代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class SoundRecording : MonoBehaviour {
public static Text t;
GameObject obj;
//中文:zh_cn 英文:en_us
private string audio_path = null;
private const string app_id = "appid = 5ac9f8d1";
private const string session_begin_params = "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding = utf-8";
public AudioSource audio;//存儲錄製的音頻
private int frequency = 16000;//採樣率
void Start()
{
obj= GameObject.FindGameObjectWithTag("Text");
t = obj.GetComponent<Text>();
}
//開始錄音、播放音頻
public void StartRecorde() {
audio.Stop();//停止上一個音頻的播放
audio.clip = Microphone.Start(null, false, 5, frequency);//開始錄製音頻,3秒
// Microphone.GetPosition(null);//得到當前所處的錄音樣本的位置
while (Microphone.GetPosition(null) <= 0) { Debug.Log("null"); }//防止音頻播放出錯
}
//停止錄音、停止播放音頻
public void StopRecorde() {
if (Microphone.IsRecording(null)) { Microphone.End(null); }//如果麥克沒有在錄製音頻
Debug.Log("開始播放音頻");
audio.Play();
// audio.Stop();//停止播放音頻
Debug.Log("停止錄音");
byte[] AudioData= Float2Byte();
//調用語音識別函數
Debug.Log("開始語音識別");
Inter_Audio_word.init_audio(app_id, session_begin_params,AudioData);
}
//將clip中的float音頻數據存儲到byte數組中
public byte[] Float2Byte() {
if (audio.clip == null) {
Debug.Log("clip is null!");
return null;
}
float[] samples = new float[audio.clip.samples];
audio.clip.GetData(samples, 0);
short[] intData = new short[samples.Length];
//converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
byte[] bytesData = new byte[samples.Length * 2];
//bytesData array is twice the size of
//dataSource array because a float converted in Int16 is 2 bytes.
int rescaleFactor = 32767; //to convert float to Int16
for (int i = 0; i < samples.Length; i++)
{
intData[i] = (short)(samples[i] * rescaleFactor);
byte[] byteArr = new byte[2];
byteArr = BitConverter.GetBytes(intData[i]);
byteArr.CopyTo(bytesData, i * 2);
}
return bytesData;
}
}
案列demo:https://download.csdn.net/download/hyy_sui_yuan/10739157