AudioEngine-inl.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /****************************************************************************
  2. Copyright (c) 2014-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. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  22. #ifndef __AUDIO_ENGINE_INL_H_
  23. #define __AUDIO_ENGINE_INL_H_
  24. #include <SLES/OpenSLES.h>
  25. #include <SLES/OpenSLES_Android.h>
  26. #include <string>
  27. #include <unordered_map>
  28. #include "base/CCRef.h"
  29. #include "base/ccUtils.h"
  30. #define MAX_AUDIOINSTANCES 24
  31. #define ERRORLOG(msg) log("fun:%s,line:%d,msg:%s",__func__,__LINE__,#msg)
  32. NS_CC_BEGIN
  33. class EventCustom;
  34. class EventListener;
  35. namespace experimental {
  36. class IAudioPlayer;
  37. class AudioPlayerProvider;
  38. class AudioEngineImpl;
  39. class AudioEngineImpl : public cocos2d::Ref
  40. {
  41. public:
  42. AudioEngineImpl();
  43. ~AudioEngineImpl();
  44. bool init();
  45. int play2d(const std::string &fileFullPath ,bool loop ,float volume);
  46. void setVolume(int audioID,float volume);
  47. void setLoop(int audioID, bool loop);
  48. void pause(int audioID);
  49. void resume(int audioID);
  50. void stop(int audioID);
  51. void stopAll();
  52. float getDuration(int audioID);
  53. float getCurrentTime(int audioID);
  54. bool setCurrentTime(int audioID, float time);
  55. void setFinishCallback(int audioID, const std::function<void (int, const std::string &)> &callback);
  56. void uncache(const std::string& filePath);
  57. void uncacheAll();
  58. void preload(const std::string& filePath, const std::function<void(bool)>& callback);
  59. void setAudioFocusForAllPlayers(bool isFocus);
  60. private:
  61. void onEnterBackground(EventCustom* event);
  62. void onEnterForeground(EventCustom* event);
  63. // engine interfaces
  64. SLObjectItf _engineObject;
  65. SLEngineItf _engineEngine;
  66. // output mix interfaces
  67. SLObjectItf _outputMixObject;
  68. //audioID,AudioInfo
  69. std::unordered_map<int, IAudioPlayer*> _audioPlayers;
  70. std::unordered_map<int, std::function<void (int, const std::string &)>> _callbackMap;
  71. // UrlAudioPlayers which need to resumed while entering foreground
  72. std::unordered_map<int, IAudioPlayer*> _urlAudioPlayersNeedResume;
  73. AudioPlayerProvider* _audioPlayerProvider;
  74. EventListener* _onPauseListener;
  75. EventListener* _onResumeListener;
  76. int _audioIDIndex;
  77. bool _lazyInitLoop;
  78. };
  79. #endif // __AUDIO_ENGINE_INL_H_
  80. }
  81. NS_CC_END
  82. #endif