Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback when file is done playing not working ! #147

Open
web3-sante opened this issue Feb 27, 2018 · 3 comments
Open

Callback when file is done playing not working ! #147

web3-sante opened this issue Feb 27, 2018 · 3 comments

Comments

@web3-sante
Copy link

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
Copy link

boubbin commented Feb 15, 2020

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
Copy link

khushbu-mulani commented Mar 23, 2020

@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
Copy link

rodrigograca31 commented Jun 13, 2022

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants