1
votes

I have built a musical webapp with Cordova (7.1.0) and I use the web audio API to manage sounds. My question is:

How can I check which audio source the player has been choose? Built-in audio VS bluetooth audio.

For example on the iPhone, during playing a game I can select to play sounds directly via my devices speakers (built-in speakers) or I can switch to my bluetooth speakers. How could I check via JavaScript when the user is switching from one mode to the other one?

I found a Cordova plugin about listening the headset state (plugged or not). This works well for wired headphones, but it doesn't work to detect when built-in speaker are switched to bluetooth speaker.

I found a Cordova plugin about listening the bluetooth state (connected or not). But this doesn't tell me when the user is switching mode...

Any ideas would be welcome!

1

1 Answers

1
votes

This Cordova plugin might be what you are looking for? https://github.com/saghul/cordova-plugin-audioroute

It's supposed to hook onto AVAudioSessionRouteChangeNotification and detect when the system audio route changes. According to the README You should be able to add the plugin and then place an event listener inside your project like so:

document.addEventListener('audioroute-changed',
  function(event) {
    console.log('Audio route changed: ' + event.reason);
    // code for stuff you want to do
  }
);

AVAudioSessionRouteChangeNotification is also mentioned here in a similar question: Detect (listen to) audio route change in iOS 7

AVAudioSessionRouteChangeNotification documentation: https://developer.apple.com/documentation/avfoundation/avaudiosessionroutechangenotification