gpt4 book ai didi

android - 将音频从内部麦克风流式传输到 3.5 毫米耳机插孔

转载 作者:行者123 更新时间:2023-11-29 01:51:55 24 4
gpt4 key购买 nike

我有一个简单的问题:

如何将 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







}


}

但我正在考虑将它放入实现线程的服务中,这样我就可以轻松启动/停止

最佳答案

为此,您必须先从移动麦克风录制声音,然后同时在您的手机上播放。为了录制工作,请在线程 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);

}

关于android - 将音频从内部麦克风流式传输到 3.5 毫米耳机插孔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com