您好我正在构建一个应用程序,它将使用来自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函数返回的值。
答案 0 :(得分:1)
通过MIC录音让我头疼:)
希望这会有所帮助,因为我整天都在玩它。 以下是MIC工作必须满足的步骤
Maniefst.xml必须包含:
&LT; uses-permission android:name =“android.permission.RECORD_AUDIO”/&gt;
应使用AudioRecord.getMinBufferSize设置AudioRecord缓冲区(你做对了)
如果未发布MIC,您将无法再次绑定到MIC! 重新启动手机将重置MIC。
以下是我如何做到的简短示例。
private android.media.AudioRecord aRecorder = null;
private Boolean breakLoop = false;
private byte[] buffer;
public Boolean StartRecording()
int freq = 22050;
try
{
prepareWaveFile(MyRandomAccessFile); //open file and writes WAV header
int bufferSize = android.media.AudioRecord.getMinBufferSize(freq, android.media.AudioFormat.CHANNEL_IN_MONO, android.media.AudioFormat.ENCODING_PCM_16BIT);
buffer = new byte[bufferSize];
if(bufferSize == AudioRecord.ERROR_BAD_VALUE){
Log.e(LOG_TAG, "Min buff size error");
return false;
}
aRecorder = new android.media.AudioRecord(MediaRecorder.AudioSource.MIC, freq, android.media.AudioFormat.CHANNEL_IN_MONO, android.media.AudioFormat.ENCODING_PCM_16BIT, bufferSize);
aRecorder.startRecording();
breakLoop = false;
int TotalSize=0; //koliko je snimljeno podataka
while (TotalSize < freq * 2 * 1 * 30) { //SampleRate * BytesPerSample * NumberOfChannels * seconds
int bufferReadResult = aRecorder.read(buffer, 0, bufferSize);
TotalSize+=bufferReadResult; // filesize failsafe
if (breakLoop) break; //if other thread stops recording
if (bufferReadResult>0) MyRandomAccessFile.write(buffer, 0, bufferReadResult); //write into some file ...
}
aRecorder.stop();
aRecorder.release(); //this is mandatory !
updateWaveFile(MyRandomAccessFile); //updates wave header
MyRandomAccessFile.close();
return true;
} catch (Exception e)
{
Log.e(LOG_TAG, "StartRecording: LOOP " + e);
try
{
aRecorder.stop();
} catch(Exception ex) {}
try
{
aRecorder.release();
} catch(Exception ex) {}
try
{
randomAccessWriter.close();
} catch(Exception ex) {}
return false;
}
答案 1 :(得分:0)