Mike
Mike

Reputation: 16

Playing audio concurrently in Ionic with ngCordova Native Audio plugin

I'm building an Ionic app that needs to play multiple audio loops at the same time.

In my project, I've implemented the cordova native audio plugin (http://ngcordova.com/docs/plugins/nativeAudio/) and it works great when playing one sound. However when I try to play 2 sounds in a loop at the same time it only plays the second sound.

$ionicPlatform.ready(function() {
  window.plugins.NativeAudio.preloadComplex('soundA', 'media/sounds/soundA.mp3', 1, 1, 0);
  window.plugins.NativeAudio.preloadComplex('soundB', 'media/sounds/soundB.mp3', 1, 1, 0);

  window.plugins.NativeAudio.loop('soundA');
  window.plugins.NativeAudio.loop('soundB');
});

According to the plugin documentation, it should be able to handle concurrency. The example on the documentation is an example of 2 tracks playing at once. I've done the same but it isn't working. I'm testing this on Android.

If anyone has anyone knows how to fix this issue it would be greatly appreciated!

Upvotes: 0

Views: 672

Answers (1)

Felipe de Araujo
Felipe de Araujo

Reputation: 1

Try to wrap your window.plugins.NativeAudio.loop calls with $timeout. I have the feeling that preloadComplex have some delay to instantiate.

$ionicPlatform.ready(function() {
  window.plugins.NativeAudio.preloadComplex('soundA', 'media/sounds/soundA.mp3', 1, 1, 0);
  window.plugins.NativeAudio.preloadComplex('soundB', 'media/sounds/soundB.mp3', 1, 1, 0);

  $timeout(function(){
    window.plugins.NativeAudio.loop('soundA');
    window.plugins.NativeAudio.loop('soundB');
  })
});

Upvotes: 0

Related Questions