Android:需要记录麦克风输入
问题说明
有没有办法在 android 中记录麦克风输入,同时实时播放/预览?我尝试使用 AudioRecord
和 AudioTrack
来执行此操作,但问题是我的设备无法播放录制的音频文件.实际上,任何安卓播放器应用程序都无法播放录制的音频文件.
Is there a way to record mic input in android while it is being process for playback/preview in real time? I tried to use AudioRecord
and AudioTrack
to do this but the problem is that my device cannot play the recorded audio file. Actually, any android player application cannot play the recorded audio file.
另一方面,使用 Media.Recorder
进行录制会生成一个可以被任何播放器应用程序播放的良好录制的音频文件.但问题是我无法在实时录制麦克风输入时进行预览/回放.
On the other hand, Using Media.Recorder
to record generates a good recorded audio file that can be played by any player application. But the thing is that I cannot make a preview/palyback while recording the mic input in real time.
正确答案
要(几乎)实时录制和播放音频,您可以启动一个单独的线程并使用 AudioRecord
和一个 AudioTrack
.
To record and play back audio in (almost) real time you can start a separate thread and use an AudioRecord
and an AudioTrack
.
请注意反馈.如果您的设备上的扬声器开得足够大,反馈会很快变得非常糟糕.
Just be careful with feedback. If the speakers are turned up loud enough on your device, the feedback can get pretty nasty pretty fast.
/*
* 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 位 PCM 数据,并将其放入缓冲区.然后 AudioTrack
对象从该缓冲区读取数据并通过扬声器播放.SD 卡上没有您以后可以访问的文件.
The audio is not really recording to a file. The AudioRecord
object encodes the audio as 16 bit PCM data and places it in a buffer. Then the AudioTrack
object reads the data from that buffer and plays it through the speakers. There is no file on the SD card that you will be able to access later.
您无法同时从 SD 卡读取和写入文件以实时播放/预览,因此您必须使用缓冲区.
You can't read and write a file from the SD card at the same time to get playback/preview in real time, so you have to use buffers.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfkbbe
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20