UrlAudioPlayer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #pragma once
  21. #include "audio/android/IAudioPlayer.h"
  22. #include "audio/android/OpenSLHelper.h"
  23. #include "audio/android/AssetFd.h"
  24. #include <mutex>
  25. #include <vector>
  26. #include <memory>
  27. #include <thread>
  28. namespace cocos2d { namespace experimental {
  29. class ICallerThreadUtils;
  30. class AssetFd;
  31. class UrlAudioPlayer : public IAudioPlayer
  32. {
  33. public:
  34. // Override Functions Begin
  35. virtual int getId() const override
  36. { return _id; };
  37. virtual void setId(int id) override
  38. { _id = id; };
  39. virtual std::string getUrl() const override
  40. { return _url; };
  41. virtual State getState() const override
  42. { return _state; };
  43. virtual void play() override;
  44. virtual void pause() override;
  45. virtual void resume() override;
  46. virtual void stop() override;
  47. virtual void rewind() override;
  48. virtual void setVolume(float volume) override;
  49. virtual float getVolume() const override;
  50. virtual void setLoop(bool isLoop) override;
  51. virtual bool isLoop() const override;
  52. virtual float getDuration() const override;
  53. virtual float getPosition() const override;
  54. virtual bool setPosition(float pos) override;
  55. virtual void setPlayEventCallback(const PlayEventCallback &playEventCallback) override;
  56. // Override Functions EndOv
  57. private:
  58. UrlAudioPlayer(SLEngineItf engineItf, SLObjectItf outputMixObject, ICallerThreadUtils* callerThreadUtils);
  59. virtual ~UrlAudioPlayer();
  60. bool prepare(const std::string &url, SLuint32 locatorType, std::shared_ptr<AssetFd> assetFd, int start, int length);
  61. static void stopAll();
  62. void destroy();
  63. inline void setState(State state)
  64. { _state = state; };
  65. void playEventCallback(SLPlayItf caller, SLuint32 playEvent);
  66. private:
  67. SLEngineItf _engineItf;
  68. SLObjectItf _outputMixObj;
  69. ICallerThreadUtils* _callerThreadUtils;
  70. int _id;
  71. std::string _url;
  72. std::shared_ptr<AssetFd> _assetFd;
  73. SLObjectItf _playObj;
  74. SLPlayItf _playItf;
  75. SLSeekItf _seekItf;
  76. SLVolumeItf _volumeItf;
  77. float _volume;
  78. float _duration;
  79. bool _isLoop;
  80. State _state;
  81. PlayEventCallback _playEventCallback;
  82. std::thread::id _callerThreadId;
  83. std::shared_ptr<bool> _isDestroyed;
  84. friend class SLUrlAudioPlayerCallbackProxy;
  85. friend class AudioPlayerProvider;
  86. };
  87. }} // namespace cocos2d { namespace experimental {