AudioDecoderProvider.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated engine source code (the "Software"), a limited,
  6. worldwide, royalty-free, non-assignable, revocable and non-exclusive license
  7. to use Cocos Creator solely to develop games on your target platforms. You shall
  8. not use Cocos Creator software for developing other software or tools that's
  9. used for developing games. You are not granted to publish, distribute,
  10. sublicense, and/or sell copies of Cocos Creator.
  11. The software or tools in this License Agreement are licensed, not sold.
  12. Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
  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. #define LOG_TAG "AudioDecoderProvider"
  22. #include "audio/android/AudioDecoderProvider.h"
  23. #include "audio/android/AudioDecoderSLES.h"
  24. #include "audio/android/AudioDecoderOgg.h"
  25. #include "audio/android/AudioDecoderMp3.h"
  26. #include "audio/android/AudioDecoderWav.h"
  27. #include "platform/CCFileUtils.h"
  28. namespace cocos2d {
  29. AudioDecoder* AudioDecoderProvider::createAudioDecoder(SLEngineItf engineItf, const std::string &url, int bufferSizeInFrames, int sampleRate, const FdGetterCallback &fdGetterCallback)
  30. {
  31. AudioDecoder* decoder = nullptr;
  32. std::string extension = FileUtils::getInstance()->getFileExtension(url);
  33. ALOGV("url:%s, extension:%s", url.c_str(), extension.c_str());
  34. if (extension == ".ogg")
  35. {
  36. decoder = new AudioDecoderOgg();
  37. if (!decoder->init(url, sampleRate))
  38. {
  39. delete decoder;
  40. decoder = nullptr;
  41. }
  42. }
  43. else if (extension == ".mp3")
  44. {
  45. decoder = new AudioDecoderMp3();
  46. if (!decoder->init(url, sampleRate))
  47. {
  48. delete decoder;
  49. decoder = nullptr;
  50. }
  51. }
  52. else if (extension == ".wav")
  53. {
  54. decoder = new AudioDecoderWav();
  55. if (!decoder->init(url, sampleRate))
  56. {
  57. delete decoder;
  58. decoder = nullptr;
  59. }
  60. }
  61. else
  62. {
  63. auto slesDecoder = new AudioDecoderSLES();
  64. if (slesDecoder->init(engineItf, url, bufferSizeInFrames, sampleRate, fdGetterCallback))
  65. {
  66. decoder = slesDecoder;
  67. }
  68. else
  69. {
  70. delete slesDecoder;
  71. }
  72. }
  73. return decoder;
  74. }
  75. void AudioDecoderProvider::destroyAudioDecoder(AudioDecoder** decoder)
  76. {
  77. if (decoder != nullptr && *decoder != nullptr)
  78. {
  79. delete (*decoder);
  80. (*decoder) = nullptr;
  81. }
  82. }
  83. } // namespace cocos2d {