cddandroidAndroidJavaEngine.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #define LOG_TAG "cocosdenshion::android::AndroidJavaEngine"
  22. #include "audio/android/jni/cddandroidAndroidJavaEngine.h"
  23. #include <stdlib.h>
  24. #include <sys/system_properties.h>
  25. #include "audio/android/ccdandroidUtils.h"
  26. #include "audio/android/utils/Utils.h"
  27. #include "audio/include/AudioEngine.h"
  28. #include "platform/android/jni/JniHelper.h"
  29. // logging
  30. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
  31. // Java class
  32. static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
  33. using namespace cocos2d;
  34. using namespace cocos2d::experimental;
  35. using namespace CocosDenshion::android;
  36. AndroidJavaEngine::AndroidJavaEngine()
  37. : _implementBaseOnAudioEngine(false)
  38. , _effectVolume(1.f)
  39. {
  40. int sdkVer = getSDKVersion();
  41. if (sdkVer > 0)
  42. {
  43. __android_log_print(ANDROID_LOG_DEBUG, "cocos2d", "android SDK version:%d", sdkVer);
  44. if (sdkVer == 21)
  45. {
  46. _implementBaseOnAudioEngine = true;
  47. }
  48. }
  49. else
  50. {
  51. __android_log_print(ANDROID_LOG_DEBUG, "cocos2d", "%s", "Fail to get android SDK version.");
  52. }
  53. }
  54. AndroidJavaEngine::~AndroidJavaEngine()
  55. {
  56. if (_implementBaseOnAudioEngine)
  57. {
  58. stopAllEffects();
  59. }
  60. JniHelper::callStaticVoidMethod(helperClassName, "end");
  61. }
  62. void AndroidJavaEngine::preloadBackgroundMusic(const char* filePath) {
  63. std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
  64. JniHelper::callStaticVoidMethod(helperClassName, "preloadBackgroundMusic", fullPath);
  65. }
  66. void AndroidJavaEngine::playBackgroundMusic(const char* filePath, bool loop) {
  67. std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
  68. JniHelper::callStaticVoidMethod(helperClassName, "playBackgroundMusic", fullPath, loop);
  69. }
  70. void AndroidJavaEngine::stopBackgroundMusic(bool releaseData) {
  71. JniHelper::callStaticVoidMethod(helperClassName, "stopBackgroundMusic");
  72. }
  73. void AndroidJavaEngine::pauseBackgroundMusic() {
  74. JniHelper::callStaticVoidMethod(helperClassName, "pauseBackgroundMusic");
  75. }
  76. void AndroidJavaEngine::resumeBackgroundMusic() {
  77. JniHelper::callStaticVoidMethod(helperClassName, "resumeBackgroundMusic");
  78. }
  79. void AndroidJavaEngine::rewindBackgroundMusic() {
  80. JniHelper::callStaticVoidMethod(helperClassName, "rewindBackgroundMusic");
  81. }
  82. bool AndroidJavaEngine::willPlayBackgroundMusic() {
  83. return JniHelper::callStaticBooleanMethod(helperClassName, "willPlayBackgroundMusic");
  84. }
  85. bool AndroidJavaEngine::isBackgroundMusicPlaying() {
  86. return JniHelper::callStaticBooleanMethod(helperClassName, "isBackgroundMusicPlaying");
  87. }
  88. float AndroidJavaEngine::getBackgroundMusicVolume() {
  89. return JniHelper::callStaticFloatMethod(helperClassName, "getBackgroundMusicVolume");
  90. }
  91. void AndroidJavaEngine::setBackgroundMusicVolume(float volume) {
  92. JniHelper::callStaticVoidMethod(helperClassName, "setBackgroundMusicVolume", volume);
  93. }
  94. float AndroidJavaEngine::getEffectsVolume()
  95. {
  96. if (_implementBaseOnAudioEngine)
  97. {
  98. return _effectVolume;
  99. }
  100. else
  101. {
  102. return JniHelper::callStaticFloatMethod(helperClassName, "getEffectsVolume");
  103. }
  104. }
  105. void AndroidJavaEngine::setEffectsVolume(float volume)
  106. {
  107. if (_implementBaseOnAudioEngine)
  108. {
  109. if (volume > 1.f)
  110. {
  111. volume = 1.f;
  112. }
  113. else if (volume < 0.f)
  114. {
  115. volume = 0.f;
  116. }
  117. if (_effectVolume != volume)
  118. {
  119. _effectVolume = volume;
  120. for (auto it : _soundIDs)
  121. {
  122. AudioEngine::setVolume(it, volume);
  123. }
  124. }
  125. }
  126. else
  127. {
  128. JniHelper::callStaticVoidMethod(helperClassName, "setEffectsVolume", volume);
  129. }
  130. }
  131. unsigned int AndroidJavaEngine::playEffect(const char* filePath, bool loop,
  132. float pitch, float pan, float gain)
  133. {
  134. if (_implementBaseOnAudioEngine)
  135. {
  136. auto soundID = AudioEngine::play2d(filePath, loop, _effectVolume);
  137. if (soundID != AudioEngine::INVALID_AUDIO_ID)
  138. {
  139. _soundIDs.push_back(soundID);
  140. AudioEngine::setFinishCallback(soundID, [this](int id, const std::string& filePath){
  141. _soundIDs.remove(id);
  142. });
  143. }
  144. return soundID;
  145. }
  146. else
  147. {
  148. std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
  149. int ret = JniHelper::callStaticIntMethod(helperClassName, "playEffect", fullPath, loop, pitch, pan, gain);
  150. return (unsigned int)ret;
  151. }
  152. }
  153. void AndroidJavaEngine::pauseEffect(unsigned int soundID)
  154. {
  155. if (_implementBaseOnAudioEngine)
  156. {
  157. AudioEngine::pause(soundID);
  158. }
  159. else
  160. {
  161. JniHelper::callStaticVoidMethod(helperClassName, "pauseEffect", (int)soundID);
  162. }
  163. }
  164. void AndroidJavaEngine::resumeEffect(unsigned int soundID)
  165. {
  166. if (_implementBaseOnAudioEngine)
  167. {
  168. AudioEngine::resume(soundID);
  169. }
  170. else
  171. {
  172. JniHelper::callStaticVoidMethod(helperClassName, "resumeEffect", (int)soundID);
  173. }
  174. }
  175. void AndroidJavaEngine::stopEffect(unsigned int soundID)
  176. {
  177. if (_implementBaseOnAudioEngine)
  178. {
  179. AudioEngine::stop(soundID);
  180. _soundIDs.remove(soundID);
  181. }
  182. else
  183. {
  184. JniHelper::callStaticVoidMethod(helperClassName, "stopEffect", (int)soundID);
  185. }
  186. }
  187. void AndroidJavaEngine::pauseAllEffects()
  188. {
  189. if (_implementBaseOnAudioEngine)
  190. {
  191. for (auto it : _soundIDs)
  192. {
  193. AudioEngine::pause(it);
  194. }
  195. }
  196. else
  197. {
  198. JniHelper::callStaticVoidMethod(helperClassName, "pauseAllEffects");
  199. }
  200. }
  201. void AndroidJavaEngine::resumeAllEffects()
  202. {
  203. if (_implementBaseOnAudioEngine)
  204. {
  205. for (auto it : _soundIDs)
  206. {
  207. AudioEngine::resume(it);
  208. }
  209. }
  210. else
  211. {
  212. JniHelper::callStaticVoidMethod(helperClassName, "resumeAllEffects");
  213. }
  214. }
  215. void AndroidJavaEngine::stopAllEffects()
  216. {
  217. if (_implementBaseOnAudioEngine)
  218. {
  219. for (auto it : _soundIDs)
  220. {
  221. AudioEngine::stop(it);
  222. }
  223. _soundIDs.clear();
  224. }
  225. else
  226. {
  227. JniHelper::callStaticVoidMethod(helperClassName, "stopAllEffects");
  228. }
  229. }
  230. void AndroidJavaEngine::preloadEffect(const char* filePath)
  231. {
  232. if (!_implementBaseOnAudioEngine)
  233. {
  234. std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
  235. JniHelper::callStaticVoidMethod(helperClassName, "preloadEffect", fullPath);
  236. }
  237. else
  238. {
  239. AudioEngine::preload(filePath);
  240. }
  241. }
  242. void AndroidJavaEngine::unloadEffect(const char* filePath)
  243. {
  244. if (!_implementBaseOnAudioEngine)
  245. {
  246. std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
  247. JniHelper::callStaticVoidMethod(helperClassName, "unloadEffect", fullPath);
  248. }
  249. else
  250. {
  251. AudioEngine::uncache(filePath);
  252. }
  253. }