PcmAudioPlayer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. _track->onStateChanged = [this, callerThreadId](Track::State state) {
  47. // It maybe in sub thread
  48. auto func = [this, state](){
  49. // It's in caller's thread
  50. if (state == Track::State::OVER)
  51. {
  52. if (_playEventCallback != nullptr)
  53. {
  54. _playEventCallback(State::OVER);
  55. }
  56. }
  57. else if (state == Track::State::STOPPED)
  58. {
  59. if (_playEventCallback != nullptr)
  60. {
  61. _playEventCallback(State::STOPPED);
  62. }
  63. }
  64. else if (state == Track::State::DESTROYED)
  65. {
  66. delete this;
  67. }
  68. };
  69. if (callerThreadId == std::this_thread::get_id())
  70. {
  71. func();
  72. }
  73. else
  74. {
  75. _callerThreadUtils->performFunctionInCallerThread(func);
  76. }
  77. };
  78. setVolume(1.0f);
  79. return true;
  80. }
  81. void PcmAudioPlayer::rewind()
  82. {
  83. ALOGW("PcmAudioPlayer::rewind isn't supported!");
  84. }
  85. void PcmAudioPlayer::setVolume(float volume)
  86. {
  87. _track->setVolume(volume);
  88. }
  89. float PcmAudioPlayer::getVolume() const
  90. {
  91. return _track->getVolume();
  92. }
  93. void PcmAudioPlayer::setLoop(bool isLoop)
  94. {
  95. _track->setLoop(isLoop);
  96. }
  97. bool PcmAudioPlayer::isLoop() const
  98. {
  99. return _track->isLoop();
  100. }
  101. float PcmAudioPlayer::getDuration() const
  102. {
  103. return _decResult.duration;
  104. }
  105. float PcmAudioPlayer::getPosition() const
  106. {
  107. return _track->getPosition();
  108. }
  109. bool PcmAudioPlayer::setPosition(float pos)
  110. {
  111. return _track->setPosition(pos);
  112. }
  113. void PcmAudioPlayer::setPlayEventCallback(const PlayEventCallback &playEventCallback)
  114. {
  115. _playEventCallback = playEventCallback;
  116. }
  117. void PcmAudioPlayer::play()
  118. {
  119. // put track to AudioMixerController
  120. ALOGV("PcmAudioPlayer (%p) play (%s) ...", this, _url.c_str());
  121. _controller->addTrack(_track);
  122. _track->setState(Track::State::PLAYING);
  123. }
  124. void PcmAudioPlayer::pause()
  125. {
  126. ALOGV("PcmAudioPlayer (%p) pause ...", this);
  127. _track->setState(Track::State::PAUSED);
  128. }
  129. void PcmAudioPlayer::resume()
  130. {
  131. ALOGV("PcmAudioPlayer (%p) resume ...", this);
  132. _track->setState(Track::State::RESUMED);
  133. }
  134. void PcmAudioPlayer::stop()
  135. {
  136. ALOGV("PcmAudioPlayer (%p) stop ...", this);
  137. _track->setState(Track::State::STOPPED);
  138. }
  139. IAudioPlayer::State PcmAudioPlayer::getState() const
  140. {
  141. IAudioPlayer::State state = State::INVALID;
  142. if (_track != nullptr)
  143. {
  144. switch (_track->getState())
  145. {
  146. case Track::State::IDLE:
  147. state = State::INITIALIZED;
  148. break;
  149. case Track::State::PLAYING:
  150. state = State::PLAYING;
  151. break;
  152. case Track::State::RESUMED:
  153. state = State::PLAYING;
  154. break;
  155. case Track::State::PAUSED:
  156. state = State::PAUSED;
  157. break;
  158. case Track::State::STOPPED:
  159. state = State::STOPPED;
  160. break;
  161. case Track::State::OVER:
  162. state = State::OVER;
  163. break;
  164. default:
  165. state = State::INVALID;
  166. break;
  167. }
  168. }
  169. return state;
  170. }
  171. }} // namespace cocos2d { namespace experimental {