NativeInterface_SoundPool.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.IO;
  3. using Rokid.UXR.Module;
  4. using UnityEngine;
  5. namespace Rokid.UXR.Native
  6. {
  7. public partial class NativeInterface
  8. {
  9. public static partial class NativeAPI
  10. {
  11. const string _logPrefix = "RKNativeSoundPool: ";
  12. // Set DEBUG to "true" to enable activity logging
  13. static bool DEBUG = false;
  14. #if UNITY_ANDROID && !UNITY_EDITOR
  15. const int _loadPriority = 1;
  16. const int _sourceQuality = 0;
  17. static AndroidJavaObject _assetFileDescriptor;
  18. static AndroidJavaObject _assets;
  19. static AndroidJavaObject _soundPool = null;
  20. static bool _hasOBB;
  21. static int _streamMusic = new AndroidJavaClass("android.media.AudioManager").GetStatic<int>("STREAM_MUSIC");
  22. public static void makeSoundPool(int maxStreams = 16)
  23. {
  24. if (DEBUG)
  25. Debug.Log(_logPrefix + "makePool(" + maxStreams + ")");
  26. var context = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
  27. if (Application.streamingAssetsPath.Substring(Application.streamingAssetsPath.Length - 12) == ".obb!/assets")
  28. {
  29. _hasOBB = true;
  30. int versionCode = context.Call<AndroidJavaObject>("getPackageManager").Call<AndroidJavaObject>("getPackageInfo", context.Call<string>("getPackageName"), 0).Get<int>("versionCode");
  31. _assets = new AndroidJavaClass("com.android.vending.expansion.zipfile.APKExpansionSupport").CallStatic<AndroidJavaObject>("getAPKExpansionZipFile", context, versionCode, 0);
  32. }
  33. else
  34. {
  35. _hasOBB = false;
  36. _assets = context.Call<AndroidJavaObject>("getAssets");
  37. }
  38. if (_soundPool != null)
  39. _soundPool.Call("release");
  40. _soundPool = new AndroidJavaObject("com.rokid.uxr.base.util.RKAsynSoundPool", maxStreams, _streamMusic, _sourceQuality);
  41. }
  42. public static int loadSound(string audioFile, bool usePersistentDataPath = false, Action<int> callback = null)
  43. {
  44. if (DEBUG)
  45. Debug.Log(_logPrefix + "load(\"" + audioFile + "\", " + usePersistentDataPath + "\", " + callback + ")");
  46. if (_soundPool == null)
  47. throw new InvalidOperationException(_logPrefix + "Use makePool() before load()!");
  48. if (callback != null)
  49. _soundPool.Call("setOnLoadCompleteListener", new OnLoadCompleteListener(callback));
  50. if (usePersistentDataPath)
  51. return _soundPool.Call<int>("load", Path.Combine(Application.persistentDataPath, audioFile), _loadPriority);
  52. if (_hasOBB)
  53. _assetFileDescriptor = _assets.Call<AndroidJavaObject>("getAssetFileDescriptor", Path.Combine("assets", audioFile));
  54. else
  55. _assetFileDescriptor = _assets.Call<AndroidJavaObject>("openFd", audioFile);
  56. return _soundPool.Call<int>("load", _assetFileDescriptor, _loadPriority);
  57. }
  58. public static int playSound(int fileID, float leftVolume = 1, float rightVolume = -1, int priority = 1, int loop = 0, float rate = 1)
  59. {
  60. if (DEBUG)
  61. Debug.Log(_logPrefix + "play(" + fileID + ", " + leftVolume + ", " + rightVolume + ", " + priority + ", " + loop + ", " + rate + ")");
  62. if (rightVolume == -1)
  63. rightVolume = leftVolume;
  64. return _soundPool.Call<int>("play", fileID, leftVolume, rightVolume, priority, loop, rate);
  65. }
  66. public static void resumeSound(int streamID)
  67. {
  68. if (DEBUG)
  69. Debug.Log(_logPrefix + "resume(" + streamID + ")");
  70. _soundPool.Call("resume", streamID);
  71. }
  72. public static void pauseSound(int streamID)
  73. {
  74. if (DEBUG)
  75. Debug.Log(_logPrefix + "pause(" + streamID + ")");
  76. _soundPool.Call("pause", streamID);
  77. }
  78. public static void stopSound(int streamID)
  79. {
  80. if (DEBUG)
  81. Debug.Log(_logPrefix + "stop(" + streamID + ")");
  82. _soundPool.Call("stop", streamID);
  83. }
  84. public static bool unloadSound(int fileID)
  85. {
  86. if (DEBUG)
  87. Debug.Log(_logPrefix + "unload(" + fileID + ")");
  88. return _soundPool.Call<bool>("unload", fileID);
  89. }
  90. public static void releaseSoundPool()
  91. {
  92. if (DEBUG)
  93. Debug.Log(_logPrefix + "releasePool()");
  94. _soundPool.Call("release");
  95. _soundPool.Dispose();
  96. _soundPool = null;
  97. }
  98. #else
  99. public static void makeSoundPool(int maxStreams = 16)
  100. {
  101. if (DEBUG)
  102. Debug.Log(_logPrefix + "makePool(" + maxStreams + ")");
  103. }
  104. public static int loadSound(string audioFile, bool usePersistentDataPath = false, Action<int> callback = null)
  105. {
  106. if (DEBUG)
  107. Debug.Log(_logPrefix + "load(\"" + audioFile + "\", " + usePersistentDataPath + "\", " + callback + ")");
  108. return 1;
  109. }
  110. public static int playSound(int fileID, float leftVolume = 1, float rightVolume = -1, int priority = 1, int loop = 0, float rate = 1)
  111. {
  112. if (DEBUG)
  113. Debug.Log(_logPrefix + "play(" + fileID + ", " + leftVolume + ", " + rightVolume + ", " + priority + ", " + loop + ", " + rate + ")");
  114. return 0;
  115. }
  116. public static void resumeSound(int streamID)
  117. {
  118. if (DEBUG)
  119. Debug.Log(_logPrefix + "resume(" + streamID + ")");
  120. }
  121. public static void pauseSound(int streamID)
  122. {
  123. if (DEBUG)
  124. Debug.Log(_logPrefix + "pause(" + streamID + ")");
  125. }
  126. public static void stopSound(int streamID)
  127. {
  128. if (DEBUG)
  129. Debug.Log(_logPrefix + "stop(" + streamID + ")");
  130. }
  131. public static void releaseSoundPool()
  132. {
  133. if (DEBUG)
  134. Debug.Log(_logPrefix + "releasePool()");
  135. }
  136. public static bool unloadSound(int fileID)
  137. {
  138. if (DEBUG)
  139. Debug.Log(_logPrefix + "unload(" + fileID + ")");
  140. return true;
  141. }
  142. #endif
  143. }
  144. }
  145. }