使用MediaPlayer时通过听筒播放声音 [英] Play sound through earpiece when using MediaPlayer

查看:779
本文介绍了使用MediaPlayer时通过听筒播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中播放语音邮件录音.我目前的设置方式是通过扬声器播放语音邮件.能够在免提电话和听筒之间切换的最佳方法是什么.这是我设置MediaPlayer的方法:

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();

我正在为4.1及更高版本进行开发.

I am building for 4.1 plus.

推荐答案

您还需要设置音频管理器模式.然后使用 audiomgr.setSpeakerphoneOn(false) API可以进行切换.

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);

这篇关于使用MediaPlayer时通过听筒播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆