Switching between earpiece and speakerphone on button press

爷,独闯天下 提交于 2019-11-30 14:01:58

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/11674921/switching-between-earpiece-and-speakerphone-on-button-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!