Switching between earpiece and speakerphone on button press

后端 未结 1 1113
庸人自扰
庸人自扰 2020-12-31 17:10

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 fro

1条回答
  •  别那么骄傲
    2020-12-31 17:58

    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.

    0 讨论(0)
提交回复
热议问题