floatinghotpot/cordova-plugin-nativeaudio

Callback when file is done playing not working !

Opened this issue · 3 comments

In Ionic Native audio plugin
The callback methot when a file is done playing not working

playAvecIonNative() {
    this.nativeAudio.preloadComplex('id', 'assets/audio/mw1.ogg', 1, 1, 0)
      .then(this.onSuccessPreloading, this.onErrorPreload);
  }

  onSuccessPreloading = (data) => {
    this.nativeAudio.play('id']).then(this.onSuccessPlaying, this.onErrorPlay);
  }

  onSuccessPlaying = (a) => {
alert('Done Playing !');
    this.nativeAudio.unload('id');
  }

When i trigger the playAvecIonNative() method to preload and play the audio , the audio file is playing but at the same time the lart method is showing the result.

I have tested in many ways but i’ve got the same results.

It seems to be an issue with the plugin.
???

I have the same problem when i target to browser platform, not sure if this is the same in ios and android. The plugin does not officially support browser so.

Workaround: new Audio() (https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement)
If you really can't use new Audio() for play(), then propably you can still use the Audio() as a hack (does not work with streams or files with unknown duration):

    const a: HTMLAudioElement = new Audio(`filename.mp3`);

    a.addEventListener('loadeddata', () => {
      // When audio is loaded we can access its duration, save duration for later!
      console.log('duration', a.duration);
      // -> Use setTimeout with duration after this.nativeAudio.play()
    });

    // Or if you can play() with Audio, then:
    a.addEventListener('ended', () => {
      // In case you play it with a.play(), then this callbacks gets called when play ends
      console.log('done playing!');
      // 
    });

@GUEYEDSMF You probably need to implement your "onSuccessPreloading" function like this:

onSuccessPreloading = (data) => {
    this.nativeAudio.play('id',  () => { 
        //completeCallback> 
        //called when the file is done playing> 
        console.log("File done playing....")> 
    }).then(this.onSuccessPlaying, this.onErrorPlay);> 
  }

play function has signature

play(id: string, completeCallback?: Function): Promise;

where you can optionally pass a callback to be called when the file is done playing

This is still an issue!

My non working code:

this.nativeAudio
    .play(uniqueId, function () {
        console.log("finished");
    })

Im using:

ionic cordova platform add browser
ionic cordova run browser --prod

(Note: doesnt work on browser, iOS and Android are fine)
related issue: #162

also seems the plugging stopped being maintained 5 years ago 😢 :
https://github.com/floatinghotpot/cordova-plugin-nativeaudio/