嗨,我正在构建一个应用程序,它将使用来自 MIC 的传入音频并将其与存储的声音文件进行比较。目前,我正试图掌握 AudioRecord 函数中的数据在保存到字节数组时的样子。我的问题是返回的值都是零。我不知道我是否可能没有正确使用/设置 AudioRecord 功能。这是我的代码:
import android.app.Activity;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SnoreAlarmActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button start, stop;
TextView display;
Boolean rec = false;
AudioRecord snore;
byte[] arrayOfByte = new byte[16 * 1024 / 8];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button) findViewById(R.id.bStart);
stop = (Button) findViewById(R.id.bStop);
display = (TextView) findViewById(R.id.tAnswer);
start.setOnClickListener(this);
stop.setOnClickListener(this);
int i = AudioRecord.getMinBufferSize(44100, 16, 2);
snore = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, 16, 2, i);// From
// MIC,Sample
// Rate
// of
// 44100,
// Channel_IN_MONO,16Bit
// Encoding,buffer
// size
// i
new Record().start();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bStart:
rec = true;
break;
case R.id.bStop:
rec = false;
snore.stop();
snore.release();
for (int i = 0; i < 100; i++) {
int x = (int) arrayOfByte[i];
Log.w("Tag", "" + x);
}
break;
}
}
class Record extends Thread {
Record() {
}
@Override
public void run() {
// TODO Auto-generated method stub
while (rec) {
snore.startRecording();
snore.read(arrayOfByte, 0, (16 * 1024 / 8));
}
}
}
}
所以我的问题是,为什么我得零?*另外我知道由于 16Bit 编码,我应该将数据存储在一个简短的位置,稍后会这样做,只是试图了解目前从 AudioRecord 函数返回的值。