Microphone.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #if UNITY_WEBGL && !UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. namespace UnityEngine
  6. {
  7. public class Microphone
  8. {
  9. [DllImport("__Internal")]
  10. public static extern void Init();
  11. [DllImport("__Internal")]
  12. public static extern void QueryAudioInput();
  13. [DllImport("__Internal")]
  14. private static extern int GetNumberOfMicrophones();
  15. [DllImport("__Internal")]
  16. private static extern string GetMicrophoneDeviceName(int index);
  17. [DllImport("__Internal")]
  18. private static extern float GetMicrophoneVolume(int index);
  19. private static List<Action> _sActions = new List<Action>();
  20. public static void Update()
  21. {
  22. for (int i = 0; i < _sActions.Count; ++i)
  23. {
  24. Action action = _sActions[i];
  25. action.Invoke();
  26. }
  27. }
  28. public static string[] devices
  29. {
  30. get
  31. {
  32. List<string> list = new List<string>();
  33. int size = GetNumberOfMicrophones();
  34. for (int index = 0; index < size; ++index)
  35. {
  36. string deviceName = GetMicrophoneDeviceName(index);
  37. list.Add(deviceName);
  38. }
  39. return list.ToArray();
  40. }
  41. }
  42. public static float[] volumes
  43. {
  44. get
  45. {
  46. List<float> list = new List<float>();
  47. int size = GetNumberOfMicrophones();
  48. for (int index = 0; index < size; ++index)
  49. {
  50. float volume = GetMicrophoneVolume(index);
  51. list.Add(volume);
  52. }
  53. return list.ToArray();
  54. }
  55. }
  56. public static bool IsRecording(string deviceName)
  57. {
  58. return false;
  59. }
  60. public static void GetDeviceCaps(string deviceName, out int minFreq, out int maxFreq)
  61. {
  62. minFreq = 0;
  63. maxFreq = 0;
  64. }
  65. public static void End(string deviceName)
  66. {
  67. }
  68. }
  69. }
  70. #endif