PcmAudioPlayer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /****************************************************************************
  2. Copyright (c) 2016-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #define LOG_TAG "PcmAudioPlayer"
  21. #include "audio/android/cutils/log.h"
  22. #include "audio/android/PcmAudioPlayer.h"
  23. #include "audio/android/AudioMixerController.h"
  24. #include "audio/android/ICallerThreadUtils.h"
  25. namespace cocos2d { namespace experimental {
  26. PcmAudioPlayer::PcmAudioPlayer(AudioMixerController * controller, ICallerThreadUtils* callerThreadUtils)
  27. : _id(-1)
  28. , _track(nullptr)
  29. , _playEventCallback(nullptr)
  30. , _controller(controller)
  31. , _callerThreadUtils(callerThreadUtils)
  32. {
  33. ALOGV("PcmAudioPlayer constructor: %p", this);
  34. }
  35. PcmAudioPlayer::~PcmAudioPlayer()
  36. {
  37. ALOGV("In the destructor of PcmAudioPlayer (%p)", this);
  38. delete _track;
  39. }
  40. bool PcmAudioPlayer::prepare(const std::string &url, const PcmData &decResult)
  41. {
  42. _url = url;
  43. _decResult = decResult;
  44. _track = new (std::nothrow) Track(_decResult);
  45. std::thread::id callerThreadId = _callerThreadUtils->getCallerThreadId();
  46. // @note The logic may cause this issue https://github.com/cocos2d/cocos2d-x/issues/17707
  47. // Assume that AudioEngine::stop(id) is invoked and the audio is played over meanwhile.
  48. // Since State::OVER and State::DESTROYED are triggered in the audio mixing thread, it will
  49. // call 'performFunctionInCallerThread' to post events to cocos's message queue.
  50. // Therefore, the sequence in cocos's thread will be |STOP|OVER|DESTROYED|.
  51. // Although, we remove the audio id in |STOPPED| callback, because it's asynchronous operation,
  52. // |OVER| and |DESTROYED| callbacks will still be invoked in cocos's thread.
  53. // HOW TO FIX: If the previous state is |STOPPED| and the current state
  54. // is |OVER|, just skip to invoke |OVER| callback.
  55. _track->onStateChanged = [this, callerThreadId](Track::State state) {
  56. // It maybe in sub thread
  57. Track::State prevState = _track->getPrevState();
  58. auto func = [this, state, prevState](){
  59. // It's in caller's thread
  60. if (state == Track::State::OVER && prevState != Track::State::STOPPED)
  61. {
  62. if (_playEventCallback != nullptr)
  63. {
  64. _playEventCallback(State::OVER);
  65. }
  66. }
  67. else if (state == Track::State::STOPPED)
  68. {
  69. if (_playEventCallback != nullptr)
  70. {
  71. _playEventCallback(State::STOPPED);
  72. }
  73. }
  74. else if (state == Track::State::DESTROYED)
  75. {
  76. delete this;
  77. }
  78. };
  79. if (callerThreadId == std::this_thread::get_id())
  80. { // onStateChanged(Track::State::STOPPED) is in caller's (Cocos's) thread.
  81. func();
  82. }
  83. else
  84. { // onStateChanged(Track::State::OVER) or onStateChanged(Track::State::DESTROYED) are in audio mixing thread.
  85. _callerThreadUtils->performFunctionInCallerThread(func);
  86. }
  87. };
  88. setVolume(1.0f);
  89. return true;
  90. }
  91. void PcmAudioPlayer::rewind()
  92. {
  93. ALOGW("PcmAudioPlayer::rewind isn't supported!");
  94. }
  95. void PcmAudioPlayer::setVolume(float volume)
  96. {
  97. _track->setVolume(volume);
  98. }
  99. float PcmAudioPlayer::getVolume() const
  100. {
  101. return _track->getVolume();
  102. }
  103. void PcmAudioPlayer::setAudioFocus(bool isFocus)
  104. {
  105. _track->setAudioFocus(isFocus);
  106. }
  107. void PcmAudioPlayer::setLoop(bool isLoop)
  108. {
  109. _track->setLoop(isLoop);
  110. }
  111. bool PcmAudioPlayer::isLoop() const
  112. {
  113. return _track->isLoop();
  114. }
  115. float PcmAudioPlayer::getDuration() const
  116. {
  117. return _decResult.duration;
  118. }
  119. float PcmAudioPlayer::getPosition() const
  120. {
  121. return _track->getPosition();
  122. }
  123. bool PcmAudioPlayer::setPosition(float pos)
  124. {
  125. return _track->setPosition(pos);
  126. }
  127. void PcmAudioPlayer::setPlayEventCallback(const PlayEventCallback &playEventCallback)
  128. {
  129. _playEventCallback = playEventCallback;
  130. }
  131. void PcmAudioPlayer::play()
  132. {
  133. // put track to AudioMixerController
  134. ALOGV("PcmAudioPlayer (%p) play, url: %s", this, _url.c_str());
  135. _controller->addTrack(_track);
  136. _track->setState(Track::State::PLAYING);
  137. }
  138. void PcmAudioPlayer::pause()
  139. {
  140. ALOGV("PcmAudioPlayer (%p) pause, url: %s", this, _url.c_str());
  141. _track->setState(Track::State::PAUSED);
  142. }
  143. void PcmAudioPlayer::resume()
  144. {
  145. ALOGV("PcmAudioPlayer (%p) resume, url: %s", this, _url.c_str());
  146. _track->setState(Track::State::RESUMED);
  147. }
  148. void PcmAudioPlayer::stop()
  149. {
  150. ALOGV("PcmAudioPlayer (%p) stop, url: %s", this, _url.c_str());
  151. _track->setState(Track::State::STOPPED);
  152. }
  153. IAudioPlayer::State PcmAudioPlayer::getState() const
  154. {
  155. IAudioPlayer::State state = State::INVALID;
  156. if (_track != nullptr)
  157. {
  158. switch (_track->getState())
  159. {
  160. case Track::State::IDLE:
  161. state = State::INITIALIZED;
  162. break;
  163. case Track::State::PLAYING:
  164. state = State::PLAYING;
  165. break;
  166. case Track::State::RESUMED:
  167. state = State::PLAYING;
  168. break;
  169. case Track::State::PAUSED:
  170. state = State::PAUSED;
  171. break;
  172. case Track::State::STOPPED:
  173. state = State::STOPPED;
  174. break;
  175. case Track::State::OVER:
  176. state = State::OVER;
  177. break;
  178. default:
  179. state = State::INVALID;
  180. break;
  181. }
  182. }
  183. return state;
  184. }
  185. }} // namespace cocos2d { namespace experimental {