Android OpenSL ES + MediaPlayer : Very inconsistent behaviour accross devices when using VOICE stream

StackOverflow https://stackoverflow.com/questions/17926272

Question

I am trying to create a real time communication application (SIP like, Skype like, etc.) that is using Android's OpenSL ES implementation.

The thing is, I need to play some WAV audio file over the communication (I am doing this with MediaPlayer in Java).

Of course, mediaplayer works fine when OpenSL ES is not running. But when it is, all hell breaks loose : the result is strongly inconsistent across devices.

  • On Nexus 7 (4.2.2) : The wav plays as it should
  • On Nexus 4 (4.2.2) & Galaxy S3 (4.1.2) : The wav plays very low (even at the volume sets at its maximum)
  • On Galaxy S4 (4.2.2) : The wav plays very loud and saturated (even at the volume sets at its minimum)

I have created an example project to demonstrate that issue, if you play the sound (pweeet button) before starting the engine, it works. If you play it after, it depends on the device.

Here are my observations :

  • In OpenSL ES, if ONLY the player works OR the recorder, everything works as expected. It is the combination of player & recorder that makes the bug (in MainActivity.java, just comment StartPlayer() [l.47] or StartRecorder() [l.48] to see that).
  • If I disable the player enqueuing (in Audio.cpp, comment (*playerBufferQueue)->Enqueue [l.78-80]) everything works as expected.
  • If I don't set OpenSL ES to play on the voice stream (In Audio.cpp, comment (*playerConfig)->SetConfiguration [l.146-187]) everything works as expected.

Of course, nothing above is a solution as I need to...

  • record from OpenSL ES as a voice communication
  • play from OpenSL ES in the voice stream
  • play from MediaPlayer in the media Stream

...all at the same time.

Finaly, I should point out that, in the Galaxy S4, when I enqueue frame that I received from the network, OpenSL ES plays it so loud and saturated that the application is unusable. So I don't think the issue is on the MediaPlayer Java side.

Was it helpful?

Solution

Thanks to Michael comment, I have solve my problem by setting Audio mode IN_COMMUNICATION.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top