Android - Play audio from earpiece

后端 未结 7 1964
旧巷少年郎
旧巷少年郎 2020-12-02 08:27

I\'d like to play audio from a file into the phone\'s earpiece (or headset of that is connected) using the android MediaPlayer. I tried using the MODE_IN_CALL h

相关标签:
7条回答
  • 2020-12-02 08:57

    Ok in my case the issue was solved by using the below code

        mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    
        mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        mAudioManager.setSpeakerphoneOn(false);
    
    0 讨论(0)
  • 2020-12-02 09:02

    i just wanted to add a complete example for Deepak Bala's Answer. It works when not using the MediaPlayer.create() method:

        this.player = new MediaPlayer(); 
        this.player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    
        try {
            this.player.setDataSource(getContext(), Uri.fromFile(new File(m.localFileUrl)));
            this.player.prepare();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-12-02 09:03

    This method is working fine for me.

    You need to set audio manager mode too. and then using audiomgr.setSpeakerphoneOn(false) api you can toggle.

    AudioManager audiomgr = (AudioManager) 
        context.getSystemService(Context.AUDIO_SERVICE);
    audiomgr.setMode(AudioManager.MODE_IN_COMMUNICATION);
    audiomgr.setSpeakerphoneOn(false);
    
    0 讨论(0)
  • 2020-12-02 09:06

    It turns out the right way to do this is through the following code.

    Play through the ear piece

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    

    Play through the speaker phone

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    

    That is it. Any other solution (Including the one posted here -> Android - Getting audio to play through earpiece) does not work consistently across devices. In fact using MODE_IN_CALL ensures that your audio will never play on certain devices.

    Note: You will need to re-prepare the media player to change streams.

    0 讨论(0)
  • 2020-12-02 09:12

    How about this then:

    // Make sure that streams following the PHONE routing strategy aren't forced
    // to play in the speaker.
    Class audioSystemClass = Class.forName("android.media.AudioSystem");
    Method setForceUse = audioSystemClass.getMethod("setForceUse",
        int.class,
        int.class);
    // First 0 == FOR_COMMUNICATION, second 0 == FORCE_NONE.
    setForceUse.invoke(null, 0, 0);
    

    Then use STREAM_VOICE_CALL for your MediaPlayer.

    If the setForceUse call succeeds and the audio from your MediaPlayer still plays in the loudspeaker, then I'd say something is messed up in the AudioPolicyManager implementation on the phone/tablet that you're testing this on.

    0 讨论(0)
  • 2020-12-02 09:13

    I found i needed to set both the mediaplayer and Audiomanager

    private MediaPlayer mp = new MediaPlayer();
    AudioManager mAudioManager = (AudioManager) v.getContext()
                                    .getSystemService(Context.AUDIO_SERVICE);
                            mAudioManager.setSpeakerphoneOn(false);
                            mp.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    
    0 讨论(0)
提交回复
热议问题