AudioCaptureExample.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using NRKernal.Record;
  2. using System;
  3. using UnityEngine;
  4. namespace NRKernal.Experimental.NRExamples
  5. {
  6. /// <summary> A audio capture example. </summary>
  7. public class AudioCaptureExample : MonoBehaviour
  8. {
  9. /// <summary> The audio capture. </summary>
  10. NRAudioCapture m_AudioCapture = null;
  11. public NRVideoCapture.AudioState audioState = NRVideoCapture.AudioState.ApplicationAndMicAudio;
  12. void Start()
  13. {
  14. CreateAudioCaptureTest();
  15. }
  16. void Update()
  17. {
  18. if (m_AudioCapture == null)
  19. {
  20. return;
  21. }
  22. if (Input.GetKeyDown(KeyCode.R) || NRInput.GetButtonDown(ControllerButton.TRIGGER))
  23. {
  24. StartAudioCapture();
  25. }
  26. if (Input.GetKeyDown(KeyCode.T) || NRInput.GetButtonDown(ControllerButton.HOME))
  27. {
  28. StopAudioCapture();
  29. }
  30. }
  31. void CreateAudioCaptureTest()
  32. {
  33. if (m_AudioCapture != null)
  34. return;
  35. m_AudioCapture = NRAudioCapture.Create();
  36. }
  37. /// <summary> Starts audio capture. </summary>
  38. void StartAudioCapture()
  39. {
  40. if (m_AudioCapture != null)
  41. {
  42. NRDebugger.Info("Created AudioCapture Instance!");
  43. CameraParameters param = new CameraParameters();
  44. param.camMode = CamMode.None;
  45. param.frameRate = 30;
  46. // Set audio state, audio record needs the permission of "android.permission.RECORD_AUDIO",
  47. // Add it to your "AndroidManifest.xml" file in "Assets/Plugin".
  48. param.audioState = audioState;
  49. m_AudioCapture.StartAudioModeAsync(param, OnStartedAudioCaptureMode);
  50. }
  51. }
  52. /// <summary> Stops audio capture. </summary>
  53. void StopAudioCapture()
  54. {
  55. NRDebugger.Info("Stop Audio Capture!");
  56. m_AudioCapture.StopRecordingAsync(OnStoppedRecordingAudio);
  57. }
  58. /// <summary> Executes the 'started audio capture mode' action. </summary>
  59. /// <param name="result"> The result.</param>
  60. void OnStartedAudioCaptureMode(NRAudioCapture.AudioCaptureResult result)
  61. {
  62. NRDebugger.Info("Started Audio Capture Mode!");
  63. m_AudioCapture.StartRecordingAsync(string.Empty, OnStartedRecordingAudio);
  64. }
  65. /// <summary> Executes the 'stopped audio capture mode' action. </summary>
  66. /// <param name="result"> The result.</param>
  67. void OnStoppedAudioCaptureMode(NRAudioCapture.AudioCaptureResult result)
  68. {
  69. NRDebugger.Info("Stopped Audio Capture Mode!");
  70. }
  71. /// <summary> Executes the 'started recording audio' action. </summary>
  72. /// <param name="result"> The result.</param>
  73. void OnStartedRecordingAudio(NRAudioCapture.AudioCaptureResult result)
  74. {
  75. NRDebugger.Info("Started Recording Audio!");
  76. }
  77. /// <summary> Executes the 'stopped recording audio' action. </summary>
  78. /// <param name="result"> The result.</param>
  79. void OnStoppedRecordingAudio(NRAudioCapture.AudioCaptureResult result)
  80. {
  81. NRDebugger.Info("Stopped Recording Audio!");
  82. m_AudioCapture.StopAudioModeAsync(OnStoppedAudioCaptureMode);
  83. }
  84. }
  85. }