AudioEngine-inl.h 3.3 KB

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