I'm using AudioManager to play a sound through earpiece just before a call is placed so I use AudioManager setMode(MODE_IN_CALL).
I'm not having any problems except in Galaxy S3 where after playing the music correctly, ringtone sounds very distorted and very noisy. I've read 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.
So I suspect there may be signals from radio layer feeding platform mixer.
So the code I'm using is like this:
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 separated thread:
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 music is done I call:
am.setSpeakerphoneOn(speakerWasOn);
am.setMode(speakerPrevMode);
am.setBluetoothA2dpOn(bthA2dpWasOn);
am.setBluetoothScoOn(bthScoWasOn);
This is only happening on Samsung Galaxy S3 (afaik) and has been tested in S2, Huawei, SonyEricsson, and others and works correctly.
Any Ideas? Thanks
UPDATE:
I have discovered that all works fine if the thread waits 5 seconds when the music finishes and after setting AudioManager to original state.
am.setSpeakerphoneOn(speakerWasOn);
am.setMode(speakerPrevMode);
am.setBluetoothA2dpOn(bthA2dpWasOn);
am.setBluetoothScoOn(bthScoWasOn);
long ctime = System.currentTimeMillis();
while (System.currentTimeMillis() - ctime < 5000);
I had the same problem. When i commented out this line
am.setMode(AudioManager.MODE_IN_CALL);
All worked fine for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With