floatinghotpot / cordova-plugin-nativeaudio

The low latency audio plugin is designed to enable low latency and polyphonic audio from Cordova/PhoneGap applications, using a very simple and basic API.
MIT License
233 stars 290 forks source link

Callback when file is done playing not working ! #147

Open web3-sante opened 6 years ago

web3-sante commented 6 years ago

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. ???

boubbin commented 4 years ago

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!');
      // 
    });
khushbu-mulani commented 4 years ago

@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

rodrigograca31 commented 1 year ago

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/