我正在尝试编写一个实时读取麦克风数据的程序。
到目前为止,这是我的主要课程:
package com.example.mictest;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
//variables
private boolean isRecording = false;
private int audioSource = MediaRecorder.AudioSource.MIC;
private int samplingRate = 44100; /* in Hz*/
private int channelConfig = AudioFormat.CHANNEL_IN_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private int bufferSize = AudioRecord.getMinBufferSize(samplingRate, channelConfig, audioFormat);
private int sampleNumBits = 16;
private int numChannels = 1;
private byte[] data;
private TextView TV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV = (TextView) findViewById(R.id.textView1);
AudioRecord recorder = new AudioRecord(AudioSource.MIC, samplingRate, channelConfig, audioFormat, bufferSize);
TV.setText(Integer.toString(recorder.getState()));
recorder.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我已经在初始化 AudioRecord 时遇到了问题,getState() 的值为零。日志猫:
09-24 18:01:07.139: E/AudioRecord(2530): AudioFlinger could not create record track, status: -1
09-24 18:01:07.139: E/AudioRecord-JNI(2530): Error creating AudioRecord instance: initialization check failed.
09-24 18:01:07.139: E/AudioRecord-Java(2530): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
我已经在清单文件中设置了 RECORD_AUDIO 的权限。我有一个带有 API 版本 7 的三星 Galaxy Spica。
有谁知道为什么我不能让它工作?