Android - Play audio from earpiece

后端 未结 7 1985
旧巷少年郎
旧巷少年郎 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 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.

提交回复
热议问题