我有一部 2.3.3 版的 LG Android 手机。我需要在耳机插孔中连接一个麦克风,因为我想创建一个读取声音样本的应用程序。
如何以编程方式读取示例?
我有一部 2.3.3 版的 LG Android 手机。我需要在耳机插孔中连接一个麦克风,因为我想创建一个读取声音样本的应用程序。
如何以编程方式读取示例?
如果您真的想阅读 AudioSamples,我建议您使用 AudioRecord 而不是 MediaRecorder,因为它可以让您对 AudioSamples 进行更多控制...为此,您可以使用以下代码,AudioCapturer 是我用于从中获取样本的包装类AudioRecord 对象..IAudioReceiver 是一个接口,它具有处理音频数据的方法。
public class AudioCapturer implements Runnable {
private AudioRecord audioRecorder = null;
private int bufferSize;
private int samplePerSec = 16000;
private String LOG_TAG = "AudioCapturer";
private Thread thread = null;
private boolean isRecording;
private static AudioCapturer audioCapturer;
private IAudioReceiver iAudioReceiver;
private AudioCapturer(IAudioReceiver audioReceiver) {
this.iAudioReceiver = audioReceiver;
}
public static AudioCapturer getInstance(IAudioReceiver audioReceiver) {
if (audioCapturer == null) {
audioCapturer = new AudioCapturer(audioReceiver);
}
return audioCapturer;
}
public void start() {
bufferSize = AudioRecord.getMinBufferSize(samplePerSec, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
if (bufferSize != AudioRecord.ERROR_BAD_VALUE && bufferSize != AudioRecord.ERROR) {
audioRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, this.samplePerSec, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, this.bufferSize * 10); // bufferSize
// 10x
if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
Log.i(LOG_TAG, "Audio Recorder created");
audioRecorder.startRecording();
isRecording = true;
thread = new Thread(this);
thread.start();
} else {
Log.e(LOG_TAG, "Unable to create AudioRecord instance");
}
} else {
Log.e(LOG_TAG, "Unable to get minimum buffer size");
}
}
public void stop() {
isRecording = false;
if (audioRecorder != null) {
if (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
// System.out
// .println("Stopping the recorder inside AudioRecorder");
audioRecorder.stop();
}
if (audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
audioRecorder.release();
}
}
}
public boolean isRecording() {
return (audioRecorder != null) ? (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) : false;
}
@Override
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
while (isRecording && audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
short[] tempBuf = new short[Constants.FRAME_SIZE / 2];
audioRecorder.read(tempBuf, 0, tempBuf.length);
iAudioReceiver.capturedAudioReceived(tempBuf, false);
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#finalize()
*/
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("AudioCapturer finalizer");
if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
audioRecorder.stop();
audioRecorder.release();
}
audioRecorder = null;
iAudioReceiver = null;
thread = null;
}
}
现在您可以从程序的 Main 类中使用此类的对象,它将开始为您提供音频样本,您可以在 IAudioReceiver(使用这些样本的类)中处理它们。
如果您仍想使用 MediaRecorder,此链接可能对您有用,
2)我如何以编程方式读取样本?
据我所知,在 Android 中,您可以使用以下两个类之一录制音频:
用于录制音频和视频。录音控制基于一个简单的状态机
AudioRecord 类管理 Java 应用程序的音频资源,以记录来自平台音频输入硬件的音频。这是通过从 AudioRecord 对象“拉”(读取)数据来实现的。应用程序负责使用以下三种方法之一及时轮询 AudioRecord 对象:read(byte[], int, int)、read(short[], int, int) 或 read(ByteBuffer, int)。选择使用哪种方法将基于 AudioRecord 用户最方便的音频数据存储格式。
Ps:按照上面的链接阅读和理解最适合您需求的链接。
1)您推荐哪种麦克风?
正如我在对您的问题的评论中提到的那样,这在 stackoverflow 上已经偏离主题,但为了完整起见: