12

在我的应用程序中,我需要将声音从麦克风直接引导到扬声器。没有其他动作。我找到了一种通过播放文件并关闭扬声器来将声音从麦克风引导到听筒的方法。所以我猜扬声器可以类似地工作。但是我不知道如何摆脱播放文件的事情。谢谢你。

speaker() {
    m_audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    m_audioManager.setSpeakerphoneOn(true);  // not needed I think
    //m_audioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); earpiece need this?
    setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    m_audioManager.setMode(AudioManager.MODE_IN_CALL);
4

1 回答 1

15

使用 AudioRecord 和 AudioTrack 录制和播放(如果需要扬声器,请更改为 ..._MUSIC

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);
     arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
     atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
     atrack.setPlaybackRate(11025);
     byte[] buffer = new byte[buffersize];
     arec.startRecording();
     atrack.play();
           while(isRecording) {
               arec.read(buffer, 0, buffersize);
               atrack.write(buffer, 0, buffer.length);
               }
     } 
于 2011-01-12T07:54:15.317 回答