我为三星Nexus S 4g写了一个录音机。代码如下。但是当我使用高于8000的采样率运行它时,它会引发错误“不支持的配置”。如果有人在Nexus 4g中打出麦克风的可录制频率范围,请告诉我。我写的代码如下:
package receive.ultrasound;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Beep_Beep_ReceiverActivity extends Activity{
private int min_buff;
private int len=0;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/demo.pcm");
AudioRecord record = null;
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonStart = (Button) findViewById(R.id.button1);
final Button buttonStop = (Button) findViewById(R.id.button2);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "button1 clicked!", 50).show();
startDownload();
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "button1 clicked!", 50).show();
stopDownload();
}
});
}
#start download function:
public void startDownload(){
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataOutputStream dos = new DataOutputStream(os);
try {
/* Create a DataOuputStream to write the audio data into the saved file.*/
min_buff = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
if( min_buff == AudioRecord.ERROR_BAD_VALUE )
Log.e( "ram", "hardware not supported for this configuration" );
if( min_buff == AudioRecord.ERROR )
Log.e( "ram", "Bad Value for \"minBufferSize\", implementation was unable to query the hardware for its output properties" );
#This is where i give the sample rate.
AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, min_buff);
byte[] buffer = new byte[min_buff * 10];
byte[] temp_buffer = new byte[min_buff];
record.startRecording();
while (len < (min_buff * 10)) {
int bufferReadResult = record.read(temp_buffer, 0, min_buff);
for (int i = 0; i < bufferReadResult; i++)
dos.writeByte(temp_buffer[i]);
len += min_buff;
}
// record.stop();
// dos.close();
}catch (Exception e) {
}
}
public void stopDownload(){
System.exit(0);
}
}