AudioManager Switches MODE_IN_CALL

I use AudioManager to play sound through the speaker before the call, so I use AudioManager setMode (MODE_IN_CALL).

I have no problems except the Galaxy S3, where, after playing correctly, the melody sounds very distorted and very noisy. I read the setMode documentation:

The audio mode encompasses audio routing AND the behavior of the telephony 
layer. Therefore this method should only be used by applications that replace
the platform-wide management of audio settings or the main telephony application.
In particular, the MODE_IN_CALL mode should only be used by the telephony
application when it places a phone call, as it will cause signals from the 
radio layer to feed the platform mixer.

Therefore, I suspect that there may be signals from the mixer of the radar feed platform.

So, the code I use is as follows:

am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
speakerWasOn = am.isSpeakerphoneOn();
speakerPrevMode = am.getMode();
bthA2dpWasOn = am.isBluetoothA2dpOn();
bthScoWasOn = am.isBluetoothScoOn();

if (BluetoothBR.bthOn && BluetoothBR.conectarBluetooth()) {
  am.setMode(AudioManager.STREAM_VOICE_CALL);
  am.setBluetoothA2dpOn(true);
  am.setBluetoothScoOn(true);

} else {
  am.setSpeakerphoneOn(false);
  am.setMode(AudioManager.MODE_IN_CALL);
}

Then I use MediaPlayer to play the .mp3 file in a separate stream:

private OnPreparedListener opl = new OnPreparedListener() {
  public void onPrepared(MediaPlayer mp) {
    try {
      mp.setVolume(1.0f, 1.0f);
      mp.start();

    } catch (Throwable e) {
      e.printStackTrace();
      mp.release();
    }
  }
};

private OnCompletionListener ocl = new OnCompletionListener() {
  public void onCompletion(MediaPlayer mp) {
    stop();
  }
};

public void run() {
  synchronized (mp) {
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(getMp3FilePath());

      mp.setDataSource(fis.getFD());
      mp.setOnPreparedListener(opl);
      mp.setOnCompletionListener(ocl);
      mp.prepare();
      fis.close();

    } catch (Exception e) {
      mp.release();
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }
  }
}

public void stop() {
  if (mp != null) {
    try {
      mp.stop();
      mp.release();
      mp.setVolume(0.5f, 0.5f);
      mp.reset();

    } catch (Exception ignored) {
    }
  }

  if (BluetoothBR.bthOn) {
    BluetoothBR.desconectarBluetooth();
  }
}

And when the music is complete, I call:

am.setSpeakerphoneOn(speakerWasOn);
am.setMode(speakerPrevMode);
am.setBluetoothA2dpOn(bthA2dpWasOn);
am.setBluetoothScoOn(bthScoWasOn);

This only happens on the Samsung Galaxy S3 (afaik) and is tested on S2, Huawei, SonyEricsson and others and works correctly.

Any ideas? Thanks

UPDATE:

, , 5 , AudioManager .

am.setSpeakerphoneOn(speakerWasOn);
am.setMode(speakerPrevMode);
am.setBluetoothA2dpOn(bthA2dpWasOn);
am.setBluetoothScoOn(bthScoWasOn);

long ctime = System.currentTimeMillis();
while (System.currentTimeMillis() - ctime < 5000);
+5
1

.

am.setMode(AudioManager.MODE_IN_CALL);

.

+1

All Articles