How to handle speaker change with Android Oboe library?

1k views Asked by At

I'm trying to implement Oboe library to my project to perform the least latency when playing sounds. I could perform playback manipulating, panning, mixing, the basics things which internal Android library such as SoundPool can provide.

I wanted to test panning effect, I connected headphones to my device, but then, I noticed that any sound didn't play through my headphones. Device speaker also didn't play any sound either. So I disconnected my headphones to check if something went wrong. I pressed sound playing button, but I couldn't hear any sound even though headphones are completely disconnected from device.

When I firstly open the app, it can play sound through device speaker without any problems, but as soon as I connect headphones, no sound plays through both device speaker and headphones.

Same for opposite, if I start app with headphones connected to device, it can play sound very well through headphones. It can even perform panning effect too, but if I disconnect headphones from device, it stops playing any sound.

  1. Do I have to re-open audio stream whenever preferred audio device is changed?
  2. If yes for 1, is there any way to notify app that audio device is changed, so I can manually close stream and re-open it? Or can I make Oboe automatically handle audio device changes?
1

There are 1 answers

1
cs guy On BEST ANSWER

You need to open a new audio stream when primary audio device changes as stated here in oboe full guide. Oboe will handle the detection of device change for you automatically if you configure it.

Disconnected audio stream An audio stream can become disconnected at any time if one of these events happens:

The associated audio device is no longer connected (for example when headphones are unplugged). An error occurs internally. An audio device is no longer the primary audio device. - When a stream is disconnected, it has the state "Disconnected" and calls to write() or other functions will return Result::ErrorDisconnected. When a stream is disconnected, all you can do is close it.

Here is an example

void OboeStreamCallback::onErrorAfterClose(AudioStream *stream, Result result) {
    if (result == oboe::Result::ErrorDisconnected) {
        LOGI("Restarting AudioStream after disconnect");
        soundEngine.restart(); // please check oboe samples for soundEngine.restart(); call
    }
}

Also there is a special case for Android P. Please read this, its important!