将音频从内置麦克风串流至3.5mm耳机插孔

时间:2013-07-06 13:23:30

标签: android audio mediarecorder

我有一个简单的问题:

如何将Android设备变为现代麦克风? ,

我不想录制音频,我只想:

  1. 在线

  2. 音频被转发到连接扬声器的3.5插孔输出(无输入),

  3. 谢谢。

    编辑:这是我正在使用的代码,感谢所有在这里贡献的人,感谢@ user1917769为您的工作答案,我没有任何延迟它的工作作为一个真正的麦克风设备:

     import android.media.AudioFormat;
     import android.media.AudioManager;
     import android.media.AudioRecord;
     import android.media.AudioTrack;
     import android.media.MediaRecorder;
     import android.media.MediaRecorder.AudioSource;
     import android.util.Log;
    
     public class TOne extends Thread {
     private Thread T1;
     private byte audiobuffer[] = new byte[20];
     public boolean Okay = true;
     public AudioRecord a;
     public AudioTrack aud;
     private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
     @Override
     public void run() {
     // TODO Auto-generated method stub
     //super.run();
    int i=AudioRecord.getMinBufferSize(8000 , AudioFormat.CHANNEL_IN_MONO ,      AudioFormat.ENCODING_PCM_16BIT);
    a= findAudioRecord();
    
    if(a.getState() != AudioRecord.STATE_INITIALIZED){
       a= findAudioRecord();  
     }
      a.startRecording();
        Log.e("Play Audio" , "Start");
    
     aud = new AudioTrack(AudioManager.STREAM_MUSIC,8000,
             AudioFormat.CHANNEL_OUT_MONO,   AudioFormat.ENCODING_PCM_16BIT,AudioTrack.getMinBufferSize(8000,AudioFormat.CHANNEL_OUT_MONO,
         AudioFormat.ENCODING_PCM_16BIT),AudioTrack.MODE_STREAM);
    
     if(aud.getState() != AudioTrack.STATE_INITIALIZED){
    aud = new AudioTrack(AudioManager.STREAM_MUSIC,8000,
             AudioFormat.CHANNEL_OUT_MONO,      AudioFormat.ENCODING_PCM_16BIT,AudioTrack.getMinBufferSize(8000,AudioFormat.CHANNEL_OUT_MONO,
             AudioFormat.ENCODING_PCM_16BIT),AudioTrack.MODE_STREAM);
    
     }
              aud.play();
             while(Okay){
    
    
                     a.read(audiobuffer,0,audiobuffer.length);
    
                            ////Log.e("IS_ACTIVE", "YES");
                             aud.write(audiobuffer,0,audiobuffer.length);
    
    
    
     }
    
    
    
     }
    
    
     public AudioRecord findAudioRecord() {
    
    for (int rate : mSampleRates) {
        for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT,      AudioFormat.ENCODING_PCM_16BIT }) {
            for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO,      AudioFormat.CHANNEL_IN_STEREO }) {
                try {
                    Log.d("C.TAG", "Attempting rate " + rate + "Hz, bits: " + audioFormat +      ", channel: "
                            + channelConfig);
                    int bufferSize = AudioRecord.getMinBufferSize(rate,      AudioFormat.CHANNEL_IN_MONO , AudioFormat.ENCODING_PCM_16BIT);
    
                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        AudioRecord recorder = new AudioRecord(AudioSource.MIC, 8000,      channelConfig, audioFormat, bufferSize);
    
                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                            return recorder;
                    }
                } catch (Exception e) {
                    Log.e("C.TAG", rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
     }
    
     private void LetsPlayThatsAudio() {
    // TODO Auto-generated method stub
    
    
    
    
    
    
    
     }
    
    
     }
    

    但是我想把它放到一个实现线程的服务中,这样我就可以轻松启动/停止

1 个答案:

答案 0 :(得分:1)

为此你必须首先从移动麦克风录制声音,然后同时在手机上播放它。对于录音工作,请在线程1中尝试这个并在audiobuffer字节数组中获取数据

        int i=AudioRecord.getMinBufferSize(8000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCO DING_PCM_16BIT);
        AudioRecord a= new AudioRecord(MediaRecorder.AudioSource.MIC,8000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT,i);
        a.startRecording();
        byte audiobuffer[]=new byte[20];
        new thread2().start();
        while(true)
        {
             a.read(audiobuffer,0,audiobuffer.length);
        }

同时播放将声音输出到3.5毫米插孔的数据尝试此代码 在主题2中

 AudioTrack aud= new AudioTrack(AudioManager.STREAM_MUSIC,8000,
 AudioFormat.CHANNEL_CONFIGURATION_MONO,       AudioFormat.ENCODING_PCM_16BIT,AudioTrack.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
 AudioFormat.ENCODING_PCM_16BIT),AudioTrack.MODE_STREAM);
 aud.play();
while(true)
{ 
    aud.write(audiobuffer,0,audiobuffer.length);

}