1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
using System; using UnityEngine;
public class MicrophoneWrapper : MonoSingleton<MicrophoneWrapper> {
? ? private string TAG = "MicrophoneWrapper: ";
? ? //標(biāo)記是否有麥克風(fēng) ? ? private bool isHaveMic = false;
? ? //當(dāng)前錄音設(shè)備名稱 ? ? string currentDeviceName = string.Empty;
? ? //錄音頻率,控制錄音質(zhì)量(8000,16000) ? ? int recordFrequency = 8000;
? ? //上次按下時間戳 ? ? double lastPressTimestamp = 0;
? ? //表示錄音的最大時長 ? ? int recordMaxLength = 10;
? ? //實際錄音長度(由于unity的錄音需先指定長度,導(dǎo)致識別上傳時候會上傳多余的無效字節(jié)) ? ? //通過該字段,獲取有效錄音長度,上傳時候剪切到無效的字節(jié)數(shù)據(jù)即可 ? ? int trueLength = 0;
? ? //存儲錄音的片段 ? ? [HideInInspector] ? ? public AudioClip saveAudioClip;
? ? // Start is called before the first frame update ? ? void Start() ? ? {
? ? }
? ? public void Init() ? ? { ? ? ? ? //獲取麥克風(fēng)設(shè)備,判斷是否有麥克風(fēng)設(shè)備 ? ? ? ? if (Microphone.devices.Length > 0) ? ? ? ? { ? ? ? ? ? ? isHaveMic = true; ? ? ? ? ? ? currentDeviceName = Microphone.devices[0]; ? ? ? ? } ? ? ? ? else { ? ? ? ? ? ? Debug.Log(TAG + " Microphone.devices is null(0) "); ? ? ? ? } ? ? }
? ? /// <summary> ? ? /// 按下錄音按鈕 ? ? /// </summary> ? ? /// <param name="eventData"></param> ? ? public void OnStartRecord() ? ? {
? ? ? ? StartRecording(); ? ? }
? ? /// <summary> ? ? /// 放開錄音按鈕 ? ? /// </summary> ? ? /// <param name="eventData"></param> ? ? public AudioClip OnStopRecord() ? ? {
? ? ? ? trueLength = EndRecording(); ? ? ? ? if (trueLength > 1) ? ? ? ? {
? ? ? ? ? ? Debug.Log(TAG+ " return AudioClip data "); ? ? ? ? ? ? return saveAudioClip; ? ? ? ? ? ? ? ? ? ? }
? ? ? ? Debug.Log(TAG + " return AudioClip is null "); ? ? ? ? return null;
? ? }
? ? /// <summary> ? ? /// 開始錄音 ? ? /// </summary> ? ? /// <param name="isLoop"></param> ? ? /// <param name="lengthSec"></param> ? ? /// <param name="frequency"></param> ? ? /// <returns></returns> ? ? private bool StartRecording(bool isLoop = false) //8000,16000 ? ? { ? ? ? ? Debug.Log(TAG+"StartRecording ? ");
? ? ? ? if (isHaveMic == false || Microphone.IsRecording(currentDeviceName)) ? ? ? ? { ? ? ? ? ? ? return false; ? ? ? ? }
? ? ? ? //開始錄音 ? ? ? ? /* ? ? ? ? ?* public static AudioClip Start(string deviceName, bool loop, int lengthSec, int frequency); ? ? ? ? ?* deviceName ? 錄音設(shè)備名稱. ? ? ? ? ?* loop ? ? ? ? 如果達(dá)到長度,是否繼續(xù)記錄 ? ? ? ? ?* lengthSec ? ?指定錄音的長度. ? ? ? ? ?* frequency ? ?音頻采樣率 ? ? ? ? ? ?*/
? ? ? ? lastPressTimestamp = GetTimestampOfNowWithMillisecond();
? ? ? ? saveAudioClip = Microphone.Start(currentDeviceName, isLoop, recordMaxLength, recordFrequency);
? ? ? ? return true; ? ? }
? ? /// <summary> ? ? /// 錄音結(jié)束,返回實際的錄音時長 ? ? /// </summary> ? ? /// <returns></returns> ? ? private int EndRecording() ? ? { ? ? ? ? Debug.Log(TAG+"EndRecording ? ");
? ? ? ? if (isHaveMic == false || !Microphone.IsRecording(currentDeviceName)) ? ? ? ? {
? ? ? ? ? ? Debug.Log(TAG + "EndRecording ?Failed ");
? ? ? ? ? ? return 0; ? ? ? ? }
? ? ? ? //結(jié)束錄音 ? ? ? ? Microphone.End(currentDeviceName);
? ? ? ? //向上取整,避免遺漏錄音末尾 ? ? ? ? return Mathf.CeilToInt((float)(GetTimestampOfNowWithMillisecond() - lastPressTimestamp) / 1000f); ? ? }
? ? /// <summary> ? ? /// 獲取毫秒級別的時間戳,用于計算按下錄音時長 ? ? /// </summary> ? ? /// <returns></returns> ? ? private double GetTimestampOfNowWithMillisecond() ? ? { ? ? ? ? return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000; ? ? } } |