3

我有一个简单的问题 :

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

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

  1. 说话

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

谢谢 。

编辑:这是我正在使用的代码,感谢所有在这里做出贡献的人,感谢@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







 }


 }

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

4

1 回答 1

2

为此,您必须先从移动麦克风录制声音,然后同时在手机上播放。对于录制工作,请在线程 1 中尝试此操作并在音频缓冲区字节数组中获取数据

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

}
于 2013-07-06T14:01:47.740 回答