I am trying to play sound with both a loudspeaker and a speaker by pressing a button between them. The problem is that I'm trying to disconnect the sound from the headphones, but nothing comes out. Then, when I press the button to switch to the speakerphone, still no sound is produced. I play from a local file.
I also have android.permission.MODIFY_AUDIO_SETTINGS in the manifest.
Here is my code:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getActivity().getBaseContext(); am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); am.setMode(AudioManager.MODE_IN_CALL); am.setSpeakerphoneOn(false); am.setBluetoothScoOn(true); speakerON = false; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonSpeaker: if(!speakerON)//speaker off { speakerON = true; am.setMode(AudioManager.MODE_NORMAL); am.setSpeakerphoneOn(true); am.setBluetoothScoOn(false); speaker.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_silent_mode_off, 0, 0, 0); } else { speakerON = false; am.setMode(AudioManager.MODE_IN_CALL); am.setSpeakerphoneOn(false); am.setBluetoothScoOn(true); speaker.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_silent_mode, 0, 0, 0); } break; } }
This is how I configure MediaPlayer:
mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), R.raw.test_message); mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); mediaPlayer.start();
android button toggle
BC2
source share