使用MediaPlayer和AudioManager在耳机和扬声器之间无法切换

时间:2019-06-21 08:03:20

标签: android android-mediaplayer android-audiomanager

我正在使用MediaPlayer通过AsyncTask播放音频,并使用AudioManager设置流。按下特定的按钮时,我叫AudioManager的{​​{1}}进行切换,但不会切换到耳机。我的音频总是通过主扬声器播放。

我尝试了这些解决方案(如下面的代码所示),但没有一个起作用:

  1. Switching between earpiece and speakerphone on button press
  2. Audiomanager Speaker not working

这是我用于初始化setSpeakerPhoneOn()中的AudioManager的代码:

onViewCreated

这是使用try { audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); audioManager.setSpeakerphoneOn(loudOn); // loudOn is defined globally, and initialized to false } 的音频播放功能:

AsyncTask

这是试图切换的按钮代码:

static private void playCall(final Context context, final String[] contactParts)
    {
        final MediaPlayer mediaPlayer = new MediaPlayer();
        new AsyncTask<Void,Void,Boolean>() {
            @Override
            protected Boolean doInBackground(Void... voids) {
                try {

                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mp.start();
                        }
                    });

                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mp.reset();
                            mp.release();
                        }
                    });

                    int audioFile_R_id = //get audio file id
                    try {
                        AssetFileDescriptor afd = context.getResources().openRawResourceFd(audioFile_R_id);
                        if (afd == null) return false;
                        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        afd.close();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
                                    .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                                    .build());

                        } else {
                            //deprecated in API 26
                            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        }

                        mediaPlayer.prepare();
                        return true;
                    }
                    catch (Resources.NotFoundException rnf)
                    {
                        rnf.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            protected void onPostExecute(Boolean prepared) {
                if (prepared) {
                    mediaPlayer.start();
                }
            }
        }.execute();
    }

1 个答案:

答案 0 :(得分:0)

更改AudioManager

    try {
            audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
            audioManager.setMode(AudioManager.MODE_IN_CALL);
            audioManager.setSpeakerphoneOn(loudOn);
            // loudOn is defined globally, and initialized to false
        }