Play sound through earpiece when using MediaPlayer

对着背影说爱祢 提交于 2019-12-12 06:20:06

问题


I am playing voicemail recordings in my app. The way I currently have it set up, it plays the voicemail through the speakerphone. What is the best way to be able to toggle between speakerphone and earpiece. Here is how I set up my MediaPlayer:

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    mediaPlayer.prepareAsync();

I am building for 4.1 plus.


回答1:


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

audiomgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audiomgr.setMode(AudioManager.STREAM_MUSIC);
audiomgr.setSpeakerphoneOn(false);


来源:https://stackoverflow.com/questions/28335355/play-sound-through-earpiece-when-using-mediaplayer

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