Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Windows.Speech;
- using System.Linq;
- using WindowsInput;
- using System.Collections.Generic;
- public class Speech : MonoBehaviour
- {
- InputSimulator IS;
- KeywordRecognizer keywordRecognizer;
- Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
- public AudioClip sound;
- static AudioSource audioSrc;
- void Start()
- {
- audioSrc = GetComponent<AudioSource>();
- IS = new InputSimulator();
- keywords.Add("mute", () =>
- {
- simulateDiscordMute();
- });
- keywords.Add("unmute", () =>
- {
- simulateDiscordUnmute();
- });
- keywords.Add("test", () =>
- {
- testSound();
- });
- keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());
- keywordRecognizer.OnPhraseRecognized += KeywordRecognizerOnPhraseRecognized;
- keywordRecognizer.Start();
- }
- void KeywordRecognizerOnPhraseRecognized(PhraseRecognizedEventArgs args) {
- System.Action keywordAction;
- if (keywords.TryGetValue(args.text, out keywordAction))
- {
- keywordAction.Invoke();
- }
- }
- private void testSound() {
- audioSrc.PlayOneShot(sound);
- }
- private void simulateDiscordMute()
- {
- IS.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.SEPARATOR);
- Debug.Log("muted");
- }
- private void simulateDiscordUnmute()
- {
- IS.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.SEPARATOR);
- Debug.Log("unmuted");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement