cordova-plugin-nativeaudio icon indicating copy to clipboard operation
cordova-plugin-nativeaudio copied to clipboard

Callback when file is done playing not working !

Open web3-sante opened this issue 7 years ago • 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. ???

web3-sante avatar Feb 27 '18 19:02 web3-sante

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!');
      // 
    });

boubbin avatar Feb 15 '20 11:02 boubbin

@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

khushbu-mulani avatar Mar 23 '20 07:03 khushbu-mulani

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: https://github.com/floatinghotpot/cordova-plugin-nativeaudio/issues/162

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

rodrigograca31 avatar Jun 13 '22 22:06 rodrigograca31