gpt4 book ai didi

Android:需要记录麦克风输入

转载 作者:IT王子 更新时间:2023-10-29 00:03:40 25 4
gpt4 key购买 nike

有没有办法在实时播放/预览过程中记录 android 中的麦克风输入?我尝试使用 AudioRecordAudioTrack 来执行此操作,但问题是我的设备无法播放录制的音频文件。实际上,任何安卓播放器应用程序都无法播放录制的音频文件。

另一方面,使用 Media.Recorder 来录制会生成一个可以由任何播放器应用程序播放的良好录制的音频文件。但问题是我无法在实时录制麦克风输入时进行预览/回放。

最佳答案

要(几乎)实时录制和播放音频,您可以启动一个单独的线程并使用 AudioRecordAudioTrack

请注意反馈。如果您的设备上的扬声器声音足够大,反馈会很快变得非常糟糕。

/*
* Thread to manage live recording/playback of voice input from the device's microphone.
*/
private class Audio extends Thread
{
private boolean stopped = false;

/**
* Give the thread high priority so that it's not canceled unexpectedly, and start it
*/
private Audio()
{
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
start();
}

@Override
public void run()
{
Log.i("Audio", "Running Audio Thread");
AudioRecord recorder = null;
AudioTrack track = null;
short[][] buffers = new short[256][160];
int ix = 0;

/*
* Initialize buffer to hold continuously recorded audio data, start recording, and start
* playback.
*/
try
{
int N = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10);
track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10, AudioTrack.MODE_STREAM);
recorder.startRecording();
track.play();
/*
* Loops until something outside of this thread stops it.
* Reads the data from the recorder and writes it to the audio track for playback.
*/
while(!stopped)
{
Log.i("Map", "Writing new data to buffer");
short[] buffer = buffers[ix++ % buffers.length];
N = recorder.read(buffer,0,buffer.length);
track.write(buffer, 0, buffer.length);
}
}
catch(Throwable x)
{
Log.w("Audio", "Error reading voice audio", x);
}
/*
* Frees the thread's resources after the loop completes so that it can be run again
*/
finally
{
recorder.stop();
recorder.release();
track.stop();
track.release();
}
}

/**
* Called from outside of the thread in order to stop the recording/playback loop
*/
private void close()
{
stopped = true;
}

}

编辑

音频并未真正录制到文件中。 AudioRecord 对象将音频编码为 16 bit PCM data并将其放在缓冲区中。然后 AudioTrack 对象从该缓冲区读取数据并通过扬声器播放。 SD 卡上没有您以后可以访问的文件。

您无法同时从 SD 卡读取和写入文件以实时播放/预览,因此您必须使用缓冲区。

关于Android:需要记录麦克风输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6959930/

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