UnityCallAndroid_Voice.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. public class UnityCallAndroid_Voice : MonoBehaviour
  7. {
  8. private AndroidJavaObject Voice;
  9. [HideInInspector]
  10. public bool isStart = false;
  11. void Start()
  12. {
  13. Func();
  14. }
  15. public void Func()
  16. {
  17. AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  18. AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
  19. Debug.unityLogger.logEnabled = true;
  20. try
  21. {
  22. Debug.Log("Voice");
  23. Voice = new AndroidJavaObject("com.iflytek.mscv5plusdemo.AsrDemo", jo);
  24. Debug.Log("Voice Has Been Called");
  25. }
  26. catch (Exception e)
  27. {
  28. Debug.LogError($"Func Expection{e}");
  29. }
  30. StartVoice();
  31. }
  32. public void StartVoice()
  33. {
  34. try
  35. {
  36. Voice.Call("StartSpeechRecognition");
  37. isStart = true;
  38. }
  39. catch (Exception e)
  40. {
  41. Debug.LogError($"StartVoice Expection{e}");
  42. }
  43. }
  44. public int GetRecognitions()
  45. {
  46. return Voice.Call<int>("GetRecognition");
  47. }
  48. private void OnApplicationQuit()
  49. {
  50. Voice.Call("StopSpeechRecognition");
  51. }
  52. }