音量检测
检测当前麦克风的输入音量
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class NewBehaviourScript2 : MonoBehaviour{ private static int VOLUME_DATA_LENGTH = 128; //录制的声音长度 public float volume; //音量 public Text text; public Slider slider; private AudioClip mMicrophoneRecode; //录制的音频 private string mDeviceName; //设备名称 public int xishu=1000; private const int frequency = 44100; //码率 private const int lengthSec = 999; //录制时长 // Use this for initialization void Start () { //获取设备名称 mDeviceName = Microphone.devices[0]; //录制一段音频 mMicrophoneRecode = Microphone.Start(mDeviceName, true, lengthSec, frequency); } // Update is called once per frame void Update () { volume = GetMaxVolume(); volume*=xishu; slider.value=Mathf.Lerp(slider.value,volume/200,0.1f); text.text=volume.ToString(); } ////// 获取最大的音量 /// /// ////// 音量大小 /// private float GetMaxVolume() { float maxVolume = 0f; //用于储存一段时间内的音频信息 float[] volumeData = new float[VOLUME_DATA_LENGTH]; int offset; //获取录制的音频的开头位置 offset = Microphone.GetPosition(mDeviceName) - VOLUME_DATA_LENGTH + 1; if(offset < 0) { return 0f; } //获取数据 mMicrophoneRecode.GetData(volumeData, offset); //解析数据 for(int i = 0;i < VOLUME_DATA_LENGTH; i++) { float tempVolume = volumeData[i]; if(tempVolume > maxVolume) { maxVolume = tempVolume; } } return maxVolume; }}
关键字识别
此处利用win10自带的识别
记得引入
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.Windows.Speech;//引入命名空间 利用 5 using SpeechLib; 6 public class NewBehaviourScript1 : MonoBehaviour 7 { 8 // 短语识别器 9 private PhraseRecognizer m_PhraseRecognizer;10 // 关键字11 12 public string[] keywords;13 14 public GameObject xiangdu;15 16 // 可信度17 public ConfidenceLevel m_confidenceLevel = ConfidenceLevel.Medium;18 // Use this for initialization19 void Start ()20 {21 22 //创建一个识别器23 m_PhraseRecognizer = new KeywordRecognizer (keywords, m_confidenceLevel);24 //通过注册监听的方法25 m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;26 //开启识别器27 m_PhraseRecognizer.Start ();28 }29 30 // 当识别到关键字时,会调用这个方法31 32 private void M_PhraseRecognizer_OnPhraseRecognized (PhraseRecognizedEventArgs args)33 {34 print (args.text);35 if (args.text.Equals("小爱"))36 {37 SpVoice v = new SpVoice();38 v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);39 v.Speak("我在");40 }41 42 if (args.text.Equals("帮我倒杯水"))43 {44 SpVoice v = new SpVoice();45 v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);46 v.Speak("是的主人");47 }48 49 if (args.text.Equals("播放七里香"))50 {51 SpVoice v = new SpVoice();52 v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);53 v.Speak("好的,主人,开始播放七里香");54 gameObject.SetActive(false);55 xiangdu.SetActive(true);56 xiangdu.GetComponent().Play();57 }58 59 if (args.text.Equals("哈哈"))60 {61 SpVoice v = new SpVoice();62 v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);63 v.Speak("主人,我没听懂");64 gameObject.SetActive(false);65 xiangdu.SetActive(true);66 xiangdu.GetComponent ().Play();67 }68 }69 private void OnDestroy ()70 {71 //用完应该释放,否则会带来额外的开销72 m_PhraseRecognizer.Dispose ();73 }74 // Update is called once per frame75 void Update ()76 {77 78 }79 80 81 82 }