我正在开发一个通过MIC记录并改变其频率的Android应用程序。类似于iphone的Helium booth应用程序。目前我已编写代码来记录文件并将其保存在SD卡中。我收到的错误是:
Could not get audio input for record source 1
Error creating AudioRecord instance: initialization check failed.
[ android.media.AudioRecord ] Error code -20 when initializing native
FATAL EXCEPTION: Thread-10
java.lang.IllegalStateException: startRecording() called on an uninitialized
以下是我的代码:
public class MainActivity extends Activity {
Integer[] freqset = {8000};
private ArrayAdapter<Integer> adapter;
Spinner spFrequency;
Button startRec, stopRec, playBack;
Boolean recording;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startRec = (Button)findViewById(R.id.startrec);
stopRec = (Button)findViewById(R.id.stoprec);
playBack = (Button)findViewById(R.id.playback);
startRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread recordThread = new Thread(new Runnable(){
@Override
public void run() {
recording = true;
startRecord();
}
});
recordThread.start();
}
});
stopRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recording = false;
}
});
playBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playRecord();
}
});
spFrequency = (Spinner)findViewById(R.id.frequency);
adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, freqset);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spFrequency.setAdapter(adapter);
}
private void startRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int BytesPerElement = 2;
int sampleFreq = (Integer)spFrequency.getSelectedItem();
Log.e("Test","Start Recording"+sampleFreq);
try {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferSize = AudioRecord.getMinBufferSize(sampleFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
Log.i("Test","Buffer Size is"+minBufferSize);
short[] audioData = new short[minBufferSize];
Log.i("Test","audioData initialized"+audioData);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize*BytesPerElement);
Log.i("Test","audioRecord initialized"+audioRecord);
audioRecord.startRecording();
while(recording){
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++){
dataOutputStream.writeShort(audioData[i]);
Log.i("Test","Recording buzzz");
}
}
audioRecord.stop();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("Testing", "Esception Found");
}
}
void playRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
while(dataInputStream.available() > 0){
audioData[i] = dataInputStream.readShort();
i++;
}
dataInputStream.close();
int sampleFreq = (Integer)spFrequency.getSelectedItem();
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
P.S我之前问了类似的问题,但我使用的是MediaRecorder。我现在正在使用AudioRecord课程。