9

I am trying to play audio from both the speakerphone and earpiece by having a button toggle between the two. The problem is that I am trying to default the audio to play from the earpiece, but nothing comes out. Then when I press the button to toggle to speakerphone, still no audio plays. I am playing from a local raw file.

I have android.permission.MODIFY_AUDIO_SETTINGS in the Manifest as well.

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;

    }
}

Here is how I am setting up the MediaPlayer:

mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), R.raw.test_message);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mediaPlayer.start();
BC2
  • 892
  • 1
  • 7
  • 23

1 Answers1

11

It turns out that I had set the mode wrong.

Here is the updated media player:

mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), R.raw.test_message);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.start();

And then I set the mode for the audio manager to :

context = getActivity().getBaseContext();

am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);        
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);

And then it worked. So make sure that the media player and audio manager are in the same mode.

shane
  • 415
  • 5
  • 18
BC2
  • 892
  • 1
  • 7
  • 23
  • 1
    According to the javadocs the call to AudioManager.setMode(..) only takes the following constants: MODE_NORMAL, MODE_RINGTONE, MODE_IN_CALL or MODE_IN_COMMUNICATION. – Ivan Dec 01 '12 at 21:33
  • 3
    basically, AudioManager.STREAM_MUSIC and MODE_IN_CALL are the same value. It should have been MODE_IN_CALL. It works in the above case because I believe both have the value "3". However, one should be using MODE_IN_CALL as Ivan points out. – Geebs Feb 20 '13 at 13:20
  • Hello Mike I have to switch between speaker mode and earpiece mode on click of a button while playing a mp3 stored on sdcard. I tried your above code but am not able to do it. Can you please elaborate the click handling? – seema Mar 20 '14 at 12:25
  • do we need to prepare mediaplayer again after switching audio manager mode? – seema Mar 20 '14 at 12:27
  • I honestly may not be the best source of help. I have moved on to other projects that are not Windows phone. I wish you the best of luck. – BC2 Apr 30 '14 at 22:04
  • This works. media player and audio manager both should be in the same mode. – Amit Thaper Nov 25 '16 at 10:13