感谢http://blog.sina.com.cn/s/blog_4c070656010127tn.html提供的AudioRecoder类的介绍。
原程序:
public class myAudioRecorder extends Activity {
private boolean isRecording = false ;
private Object tmp = new Object() ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.start_bt) ;
start.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Thread thread = new Thread(new Runnable() {
public void run() {
record();
}
});
thread.start();
findViewById(R.id.start_bt).setEnabled(false) ;
findViewById(R.id.end_bt).setEnabled(true) ;
}
}) ;
Button play = (Button)findViewById(R.id.play_bt) ;
play.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play();
}
}) ;
Button stop = (Button)findViewById(R.id.end_bt) ;
stop.setEnabled(false) ;
stop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isRecording = false ;
findViewById(R.id.start_bt).setEnabled(true) ;
findViewById(R.id.end_bt).setEnabled(false) ;
}
}) ;
}
public void play() {
// Get the file we want to playback.
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
// Get the length of the audio stored in the file (16 bit so 2 bytes per short)
// and create a short array to store the recorded audio.
int musicLength = (int)(file.length()/2);
short[] music = new short[musicLength];
try {
// Create a DataInputStream to read the audio data back from the saved file.
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
// Read the file into the music array.
int i = 0;
while (dis.available() > 0) {
music[i] = dis.readShort();
i++;
}
// Close the input streams.
dis.close();
// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file.
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
musicLength*2,
AudioTrack.MODE_STREAM);
// Start playback
audioTrack.play();
// Write the music buffer to the AudioTrack object
audioTrack.write(music, 0, musicLength);
audioTrack.stop() ;
} catch (Throwable t) {
Log.e("AudioTrack","Playback Failed");
}
}
public void record() {
int frequency = 11025;
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
// Delete any previous recording.
if (file.exists())
file.delete();
// Create the new file.
try {
file.createNewFile();
} catch (IOException e) {
throw new IllegalStateException("Failed to create " + file.toString());
}
try {
// Create a DataOuputStream to write the audio data into the saved file.
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
// Create a new AudioRecord object to record the audio.
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration,
audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
isRecording = true ;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++)
dos.writeShort(buffer[i]);
}
audioRecord.stop();
dos.close();
} catch (Throwable t) {
Log.e("AudioRecord","Recording Failed");
}
}
}
由于百度媒体云的语音识别只支持小端字节序的音频,而使用上面的方法输出的文件是大端字节序,所以要对record函数进行一下修改。(单声道的API已经被划横线,下面使用了新的单声道API,相关参数也已经调为百度提供的DEMO使用的参数):
public void record() {
int frequency = 8000;
int channelConfiguration = AudioFormat.CHANNEL_IN_DEFAULT;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/order.pcm");
// Delete any previous recording.
if (file.exists())
file.delete();
// Create the new file.
try {
file.createNewFile();
} catch (IOException e) {
throw new IllegalStateException("Failed to create " + file.toString());
}
try {
// Create a DataOuputStream to write the audio data into the saved file.
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
// Create a new AudioRecord object to record the audio.
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration,
audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
byte[] bt = new byte[2];
audioRecord.startRecording();
isRecording = true;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
bt[1] = (byte) (buffer[i] >> 8);
bt[0] = (byte) (buffer[i] >> 0);
dos.writeShort((short) (((bt[0] << 8) | bt[1] & 0xff)));
}
}
for (int i = 0; i <= 100; i++) {
dos.writeShort(0x00);
}
audioRecord.stop();
dos.flush();
dos.close();
} catch (Throwable t) {
Log.e("AudioRecord", "Recording Failed");
}
}