PcmAudioPlayer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 "PcmAudioPlayer"
  22. #include "audio/android/cutils/log.h"
  23. #include "audio/android/PcmAudioPlayer.h"
  24. #include "audio/android/AudioMixerController.h"
  25. #include "audio/android/ICallerThreadUtils.h"
  26. namespace cocos2d {
  27. PcmAudioPlayer::PcmAudioPlayer(AudioMixerController * controller, ICallerThreadUtils* callerThreadUtils)
  28. : _id(-1)
  29. , _track(nullptr)
  30. , _playEventCallback(nullptr)
  31. , _controller(controller)
  32. , _callerThreadUtils(callerThreadUtils)
  33. {
  34. ALOGV("PcmAudioPlayer constructor: %p", this);
  35. }
  36. PcmAudioPlayer::~PcmAudioPlayer()
  37. {
  38. ALOGV("In the destructor of PcmAudioPlayer (%p)", this);
  39. delete _track;
  40. }
  41. bool PcmAudioPlayer::prepare(const std::string &url, const PcmData &decResult)
  42. {
  43. _url = url;
  44. _decResult = decResult;
  45. _track = new (std::nothrow) Track(_decResult);
  46. std::thread::id callerThreadId = _callerThreadUtils->getCallerThreadId();
  47. // @note The logic may cause this issue https://github.com/cocos2d/cocos2d-x/issues/17707
  48. // Assume that AudioEngine::stop(id) is invoked and the audio is played over meanwhile.
  49. // Since State::OVER and State::DESTROYED are triggered in the audio mixing thread, it will
  50. // call 'performFunctionInCallerThread' to post events to cocos's message queue.
  51. // Therefore, the sequence in cocos's thread will be |STOP|OVER|DESTROYED|.
  52. // Although, we remove the audio id in |STOPPED| callback, because it's asynchronous operation,
  53. // |OVER| and |DESTROYED| callbacks will still be invoked in cocos's thread.
  54. // HOW TO FIX: If the previous state is |STOPPED| and the current state
  55. // is |OVER|, just skip to invoke |OVER| callback.
  56. _track->onStateChanged = [this, callerThreadId](Track::State state) {
  57. // It maybe in sub thread
  58. Track::State prevState = _track->getPrevState();
  59. auto func = [this, state, prevState](){
  60. // It's in caller's thread
  61. if (state == Track::State::OVER && prevState != Track::State::STOPPED)
  62. {
  63. if (_playEventCallback != nullptr)
  64. {
  65. _playEventCallback(State::OVER);
  66. }
  67. }
  68. else if (state == Track::State::STOPPED)
  69. {
  70. if (_playEventCallback != nullptr)
  71. {
  72. _playEventCallback(State::STOPPED);
  73. }
  74. }
  75. else if (state == Track::State::DESTROYED)
  76. {
  77. delete this;
  78. }
  79. };
  80. if (callerThreadId == std::this_thread::get_id())
  81. { // onStateChanged(Track::State::STOPPED) is in caller's (Cocos's) thread.
  82. func();
  83. }
  84. else
  85. { // onStateChanged(Track::State::OVER) or onStateChanged(Track::State::DESTROYED) are in audio mixing thread.
  86. _callerThreadUtils->performFunctionInCallerThread(func);
  87. }
  88. };
  89. setVolume(1.0f);
  90. return true;
  91. }
  92. void PcmAudioPlayer::rewind()
  93. {
  94. ALOGW("PcmAudioPlayer::rewind isn't supported!");
  95. }
  96. void PcmAudioPlayer::setVolume(float volume)
  97. {
  98. _track->setVolume(volume);
  99. }
  100. float PcmAudioPlayer::getVolume() const
  101. {
  102. return _track->getVolume();
  103. }
  104. void PcmAudioPlayer::setAudioFocus(bool isFocus)
  105. {
  106. _track->setAudioFocus(isFocus);
  107. }
  108. void PcmAudioPlayer::setLoop(bool isLoop)
  109. {
  110. _track->setLoop(isLoop);
  111. }
  112. bool PcmAudioPlayer::isLoop() const
  113. {
  114. return _track->isLoop();
  115. }
  116. float PcmAudioPlayer::getDuration() const
  117. {
  118. return _decResult.duration;
  119. }
  120. float PcmAudioPlayer::getPosition() const
  121. {
  122. return _track->getPosition();
  123. }
  124. bool PcmAudioPlayer::setPosition(float pos)
  125. {
  126. return _track->setPosition(pos);
  127. }
  128. void PcmAudioPlayer::setPlayEventCallback(const PlayEventCallback &playEventCallback)
  129. {
  130. _playEventCallback = playEventCallback;
  131. }
  132. void PcmAudioPlayer::play()
  133. {
  134. // put track to AudioMixerController
  135. ALOGV("PcmAudioPlayer (%p) play, url: %s", this, _url.c_str());
  136. _controller->addTrack(_track);
  137. _track->setState(Track::State::PLAYING);
  138. }
  139. void PcmAudioPlayer::pause()
  140. {
  141. ALOGV("PcmAudioPlayer (%p) pause, url: %s", this, _url.c_str());
  142. _track->setState(Track::State::PAUSED);
  143. }
  144. void PcmAudioPlayer::resume()
  145. {
  146. ALOGV("PcmAudioPlayer (%p) resume, url: %s", this, _url.c_str());
  147. _track->setState(Track::State::RESUMED);
  148. }
  149. void PcmAudioPlayer::stop()
  150. {
  151. ALOGV("PcmAudioPlayer (%p) stop, url: %s", this, _url.c_str());
  152. _track->setState(Track::State::STOPPED);
  153. }
  154. IAudioPlayer::State PcmAudioPlayer::getState() const
  155. {
  156. IAudioPlayer::State state = State::INVALID;
  157. if (_track != nullptr)
  158. {
  159. switch (_track->getState())
  160. {
  161. case Track::State::IDLE:
  162. state = State::INITIALIZED;
  163. break;
  164. case Track::State::PLAYING:
  165. state = State::PLAYING;
  166. break;
  167. case Track::State::RESUMED:
  168. state = State::PLAYING;
  169. break;
  170. case Track::State::PAUSED:
  171. state = State::PAUSED;
  172. break;
  173. case Track::State::STOPPED:
  174. state = State::STOPPED;
  175. break;
  176. case Track::State::OVER:
  177. state = State::OVER;
  178. break;
  179. default:
  180. state = State::INVALID;
  181. break;
  182. }
  183. }
  184. return state;
  185. }
  186. } // namespace cocos2d {