Search code examples
androidionic2cordova-plugins

ionic 2 - play mp3 file


I use https://ionicframework.com/docs/native/media/ plugin for play mp3 file in

ionic 2 project like this code : (put animal audio file in src/assets/audio/animal.mp3)

play(){
    const file: MediaObject = this.media.create('../assets/audio/animal.mp3');
    file.play();
  }

in html

 <button  (click) = "play()" >Paly</button>

but in android device and when click on play button , I cannot hear any sounds


Solution

  • Try to use NativeAudio plugin (https://ionicframework.com/docs/native/native-audio/). It works really nice for me. Some example:

    if (this.platform.is('cordova')) {
      this.nativeAudio.preloadSimple('chamada', 'assets/sounds/Umbriel.mp3');
      this.nativeAudio.loop('chamada');
    }
    

    In this case, nativeAudio is injected NativeAudio module. To stop it, I do:

      if (this.platform.is('cordova')) {
        this.nativeAudio.stop('chamada');
        this.nativeAudio.unload('chamada');
      }
    

    You can check more options on documentation. Give it a try... good luck!