11

I search a lot about my problem and I found no solution. In my app using Cordova, I play mp3 sounds. Everything is fine except when you played background music (mp3s, google music, ...), the sound stop the background music. I would like to not stop the background music when my sound is played.

I tried 3 differents methods:

1 - Plugin: cordova-plugin-media

When the sound play, the background music stops and do not restart at the end.

2 - Plugin: cordova-plugin-nativeaudio

Worst, the background music stops when I launch my application

3 - Using javascript function

var mySound = new Audio('sounds/entering.mp3');
mySound.play();

Works fine on Android, the sound is playing, the background music volume decrease and after the sound is finished, the volume of the background music come back to normal. On iOS, the background music stops and do not replay at the end.

Does anyone already had this problem?

Thanks!

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
PatrickB
  • 313
  • 3
  • 14
  • Have you found a solution for this? – m1crdy Nov 02 '16 at 13:11
  • I use not a clean solution. I use cordova-plugin-yanap on Android device. It works fine. On iOS, I use the ordova-plugin-dialogs and I modifiied the sound of the beep. You can find the sound file in CDVNotification.bundle... Works for me because I only have one sound... – PatrickB Dec 09 '16 at 03:45
  • `cordova-plugin-nativeaudio` has worked fine for me for what you're trying to do. – johnborges Jan 12 '17 at 14:41

1 Answers1

2

I created a cordova plugin to handle this situation: https://github.com/davidfig/cordova-allow-background-audio

cordova add plugin cordova-allow-background-audio

The plugin was based on answers I found here: iphone app allow background music to continue to play

David Figatner
  • 771
  • 8
  • 18
  • Which methods that I described should I use with your plugin? – PatrickB Aug 24 '17 at 13:29
  • If you use this iOS plugin, background music will continue to play along with your app sounds. You can use any sound library. I use howler.js in my apps. The plugin changes how the app handles sound. The default Cordova (or probably web view) setting is that when an app uses sounds it takes exclusive control over the the speaker. The plugin changes that setting. – David Figatner Aug 25 '17 at 16:18
  • This is fantastic and is exactly what I needed. One thing to note though is the AVAudioSessionCategoryAmbient is silenced when the iPhone has the silence switch set to on. I was pulling my hair out trying to figure out why the audio worked when connected to Bluetooth but not with the native speaker until I found that note in the Apple Dev docs. Not a big deal typically, but worth noting. – pk__ Sep 25 '17 at 04:57
  • Does not work in my app by some reason.. I use var mySound = new Audio('sounds/entering.mp3'); to play an audio from JS code – Rubycon Dec 13 '18 at 13:24