我有一部版本为2.3.3的LG Android手机。我需要连接耳机插孔中的麦克风,因为我想创建一个读取声音样本的应用程序。
如何以编程方式阅读样本?。
答案 0 :(得分:6)
如果您真的想阅读AudioSamples,我建议您使用AudioRecord而不是MediaRecorder,因为它可以让您更好地控制AudioSamples ...为此您可以使用以下代码,AudioCapturer是我用于获取的包装类来自AudioRecord object..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,this link对您有用,
答案 1 :(得分:1)
2)我如何以编程方式阅读样本?
据我所知,在Android中,您可以使用以下两个类之一录制音频:
用于录制音频和视频。录制控制基于简单的状态机
AudioRecord类管理Java应用程序的音频资源,以记录来自平台的音频输入硬件的音频。这是通过从AudioRecord对象“拉”(读取)数据来实现的。应用程序负责使用以下三种方法之一及时轮询AudioRecord对象:read(byte [],int,int),read(short [],int,int)或read(ByteBuffer,int)。选择使用哪种方法将基于对AudioRecord用户最方便的音频数据存储格式。
Ps:请按照上面的链接阅读并了解最符合您需求的链接。
1)你推荐哪种麦克风?
正如我在你的问题上的评论中所提到的,这在stackoverflow上得到off-topic,但为了完整性: