Android - Play audio from earpiece

后端 未结 7 1982
旧巷少年郎
旧巷少年郎 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: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();
        }
    

提交回复
热议问题