unity 简单语音功能

  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 
  5 public class test : MonoBehaviour {
  6 
  7     // Use this for initialization
  8     int timeLimit = 0;
  9     bool isStop = true;
 10     AudioSource audio=null;
 11     void Start ()
 12     {
 13         if (audio == null)
 14         {
 15             audio = gameObject.AddComponent<AudioSource>();
 16         }
 17     }
 18     
 19     // Update is called once per frame
 20     void Update () {
 21         
 22     }
 23     public void LongDown()
 24     {
 25         if (isStop)
 26         {
 27             StartRecord();
 28         }
 29         isStop = false;
 30         Debug.Log("长按");
 31     }
 32     public void StartRecord()
 33     {
 34         Debug.Log("点击");
 35         audio.Stop();
 36         audio.loop = false;
 37         audio.mute = true;
 38         audio.clip = Microphone.Start(null,false,10,441000);
 39         while (!(Microphone.GetPosition(null) > 0)) { }
 40         audio.Play();
 41         StartCoroutine(TimeDown());
 42     }
 43 
 44     private IEnumerator TimeDown()
 45     {
 46         int time = 0;
 47         while (time < 10)
 48         {
 49             if (!Microphone.IsRecording(null))
 50             {
 51                 yield break;
 52             }
 53             if (isStop)
 54             {
 55                 timeLimit = time;
 56                 yield break;
 57             }
 58             yield return new WaitForSeconds(1);
 59             time++;
 60         }
 61         if (time >= 10)
 62         {
 63             timeLimit = time;
 64             StopRecord();
 65         }
 66         yield return 0;
 67     }
 68 
 69     public void StopRecord()
 70     {
 71         isStop = true;
 72         Microphone.End(null);
 73         audio.Stop();
 74         Debug.Log("松开");
 75     }
 76 
 77     public void PlayRecord()
 78     {
 79         PlayClipData(GetClipData());
 80     }
 81     public float[] GetClipData()
 82     {
 83         if (audio.clip == null)
 84         {
 85             Debug.Log("GetClipData audio.clip is null");
 86             return null;
 87         }
 88         float[] samples = new float[audio.clip.samples];
 89         audio.clip.GetData(samples, 0);
 90         return samples;
 91     }
 92 
 93     public void PlayClipData(float[] samplesData)
 94     {
 95         if (samplesData.Length == 0)
 96         {
 97             Debug.Log("get intarr clipdata is null");
 98             return;
 99         }
100         //从float[]到Clip   
101         AudioSource audioSource = this.GetComponentInChildren<AudioSource>();
102         if (audioSource.clip == null)
103         {
104             //audioSource.clip = AudioClip.Create(selectedDevice, samplesData.Length, timeLimit, frequencyRate, false, false); 
105             audioSource.clip = Microphone.Start(null, false, timeLimit, 441000);
106             //audioSource.clip = Microphone.Start(null, true, 1, 10000);
107             //Microphone.Start(null, false, 1, int.Parse(sFrequency));
108         }
109         audioSource.clip.SetData(samplesData, 0);
110         audioSource.mute = false;
111         audioSource.Play();
112     }
113 }

UGUI Button 长按功能

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.EventSystems;
 4 using UnityEngine.Events;
 5 
 6 public class RepeatButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
 7 {
 8 
 9     public bool invokeOnce = false;//是否只调用一次  
10     private bool hadInvoke = false;//是否已经调用过  
11 
12     public float interval = 0.1f;//按下后超过这个时间则认定为"长按"  
13     private bool isPointerDown = false;
14     private float recordTime;
15 
16     public UnityEvent onPress = new UnityEvent();//按住时调用  
17     public UnityEvent onRelease = new UnityEvent();//松开时调用  
18 
19     void Start()
20     {
21 
22     }
23 
24     void Update()
25     {
26         if (invokeOnce && hadInvoke) return;
27         if (isPointerDown)
28         {
29             if ((Time.time - recordTime) > interval)
30             {
31                 onPress.Invoke();
32                 hadInvoke = true;
33             }
34         }
35     }
36 
37     public void OnPointerDown(PointerEventData eventData)
38     {
39         isPointerDown = true;
40         recordTime = Time.time;
41     }
42 
43     public void OnPointerUp(PointerEventData eventData)
44     {
45         isPointerDown = false;
46         hadInvoke = false;
47         onRelease.Invoke();
48     }
49 
50     public void OnPointerExit(PointerEventData eventData)
51     {
52         isPointerDown = false;
53         hadInvoke = false;
54         onRelease.Invoke();
55     }
56 }

 

posted @ 2017-05-16 16:19  kadajEvo  阅读(1235)  评论(0)    收藏  举报