为了账号安全,请及时绑定邮箱和手机立即绑定

Android-通过听筒播放音频

Android-通过听筒播放音频

12345678_0001 2019-12-25 15:03:01
我目前有一些代码,该代码使用AudioRecord类从设备麦克风中读取录音,然后使用AudioTrack类将其回放。我的问题是,当我播放时,它是通过扬声器播放的。我希望它通过设备上的听筒播放。这是我的代码:public class LoopProg extends Activity { boolean isRecording; //currently not used AudioManager am; int count = 0; /** Called when the activity is first created. */ @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);        am.setMicrophoneMute(true);        while(count <= 1000000){        Record record = new Record();          record.run();        count ++;        Log.d("COUNT", "Count is : " + count);        }    }    public class Record extends Thread{      static final int bufferSize = 200000;      final short[] buffer = new short[bufferSize];      short[] readBuffer = new short[bufferSize];      public void run() {        isRecording = true;      android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);      int buffersize = AudioRecord.getMinBufferSize(11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);      AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);      AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);      am.setRouting(AudioManager.MODE_NORMAL,1, AudioManager.STREAM_MUSIC);      int ok = am.getRouting(AudioManager.ROUTE_EARPIECE);      Log.d("ROUTING", "getRouting = " + ok);      setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);      //am.setSpeakerphoneOn(true);      }   } }如您所见,如果我尝试使用AudioManager类及其方法(包括不赞成使用的setRouting方法)尝试的代码没有任何效果,则setSpeakerphoneOn方法似乎完全无效,路由方法也无效。有没有人知道如何通过听筒而不是Spaker电话播放它?
查看完整描述

3 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

请使用此代码,效果很好:


    //PLAY ON EARPIECE

    mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

    audioManager.setMode(AudioManager.MODE_IN_CALL);

    audioManager.setSpeakerphoneOn(false);


    //PLAY ON SPEAKER

    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    audioManager.setMode(AudioManager.MODE_IN_CALL);

    audioManager.setSpeakerphoneOn(true);


查看完整回答
反对 回复 2019-12-25
  • 3 回答
  • 0 关注
  • 764 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信