以下程序中,录音文件是存在什么目录下? 怎么找不到?
package com.android.speechrecorder;public class SpeechRecorderActivity extends Activity {
private static final String TAG = "SpeechRecorderActivity"; private static final int DURATION_SEC = 7; private Handler mHandler; private TextView mCommand;
private TextView mStatus;
private Button mRecord;
private Button mRedo;
private RadioButton m8KHz;
private RadioButton m11KHz;
private RadioButton mCall;
private RadioButton mDialNanp;
private RadioButton mDialPairs; private InputStream mMicrophone;
private ByteArrayOutputStream mBaos; private File mUtterance;
private int mSampleRate;
private Thread mThread;
private boolean mStoppedListening; @Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle); mHandler = new Handler(); setContentView(R.layout.recorder);
mCommand = (TextView) findViewById(R.id.commandText);
mStatus = (TextView) findViewById(R.id.statusText);
mRecord = (Button) findViewById(R.id.recordButton);
mRedo = (Button) findViewById(R.id.redoButton);
m8KHz = (RadioButton)findViewById(R.id.codec8KHzRadioButton);
m11KHz = (RadioButton)findViewById(R.id.codec11KHzRadioButton);
mCall = (RadioButton)findViewById(R.id.callRadioButton);
mDialNanp = (RadioButton)findViewById(R.id.dialNanpRadioButton);
mDialPairs = (RadioButton)findViewById(R.id.dialPairsRadioButton); mCommand.setText("Please click 'Record' to begin");
mRecord.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (Config.LOGD) {
Log.d(TAG, "mRecord.OnClickListener.onClick");
} setupRecording();
}
}); mRedo.setEnabled(false);
mRedo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (Config.LOGD) {
Log.d(TAG, "mRedo.onClickListener.onClick");
} mUtterance.delete(); setupRecording();
}
}); m8KHz.setText("PCM/16bit/8KHz");
m11KHz.setText("PCM/16bit/11KHz");
m11KHz.setChecked(true);
mCall.setChecked(true);
} private void setupRecording() {
Log.d(TAG, "setupRecording");
// disable buttons
mRedo.setEnabled(false);
mRecord.setEnabled(false);
m8KHz.setFocusable(false);
m11KHz.setFocusable(false);
mCall.setFocusable(false);
mDialNanp.setFocusable(false);
mDialPairs.setFocusable(false); // find the first utterance not covered
String[] utterances = mCall.isChecked() ? mCallUtterances :
mDialNanp.isChecked() ? mDialNanpUtterances :
mDialPairs.isChecked() ? mDialPairsUtterances :
null;
mUtterance = null;
int index = -1;
for (int i = 0; i < utterances.length; i++) {
File u = new File(getDir("recordings", MODE_PRIVATE),
utterances[i].toLowerCase().replace(' ', '_') + ".wav");
if (!u.exists()) {
mUtterance = u;
index = i;
break;
}
} // check if done
if (mUtterance == null) {
mCommand.setText("Finished: Thank You!");
return;
}
Log.d(TAG, "going to record " + mUtterance.toString()); // fix up UI
mCommand.setText("Say: \"" + utterances[index] + "\"");
final String status = "item " + (index + 1) + "/" + utterances.length; // start the microphone
mSampleRate = m8KHz.isChecked()? 8000 :
m11KHz.isChecked() ? 11025 :
11025;
mBaos = new ByteArrayOutputStream(mSampleRate * 2 * 20);
try {
mMicrophone = new MicrophoneInputStream(mSampleRate, mSampleRate * 15);// mMicrophone = logInputStream(mUtterance.toString(), mMicrophone, mSampleRate);
} catch (IOException e) { } // post a number of delayed events to update the UI and to stop recording
// after a few seconds.
for (int i = 0; i <= DURATION_SEC; i++) {
final int remain = DURATION_SEC - i;
mHandler.postDelayed(new Runnable() {
public void run() {
if (remain > 0) {
mStatus.setText(status + " Recording... " + remain);
}
else {
mStatus.setText(status);
stopRecording();
}
}
}, i * 1000);
} // now start a thread to store the audio.
mStoppedListening = false;
mThread = new Thread() {
public void run() {
Log.d(TAG, "run audio capture thread");
byte buffer[] = new byte[512];
while (!mStoppedListening) {
try {
int rtn = 0;
rtn = mMicrophone.read(buffer, 0, 512);
if (rtn > 0) mBaos.write(buffer, 0, rtn);
} catch (IOException e) {
}
}
}
};
mThread.start(); // to avoid the button click
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
} } private void stopRecording() {
Log.d(TAG, "stopRecording");
mStoppedListening = true;
try {
mThread.join();
} catch (InterruptedException e) { }
try {
OutputStream out = new FileOutputStream(mUtterance.toString());
try {
byte[] pcm = mBaos.toByteArray();
Log.d(TAG, "byteArray length " + pcm.length);
WaveHeader hdr = new WaveHeader(WaveHeader.FORMAT_PCM,
(short)1, mSampleRate, (short)16, pcm.length);
hdr.write(out);
out.write(pcm);
} finally {
out.close();
mMicrophone.close();
mBaos.close();
}
} catch (IOException e) {
} finally {
} // stop the recording
mRecord.setEnabled(true); mRedo.setEnabled(true); mCommand.setText("Got it!");
}
private final static String[] mCallUtterances = new String[] {
"Call Adam Varro",
"Call Alex Lloyd",
}; private final static String[] mDialPairsUtterances = new String[] {
// all possible pairs
"Dial 000 000 0000",
"Dial 101 010 1010",
};
private final static String[] mDialNanpUtterances = new String[] {
"Dial 211",
"Dial 411",
};
}
package com.android.speechrecorder;public class SpeechRecorderActivity extends Activity {
private static final String TAG = "SpeechRecorderActivity"; private static final int DURATION_SEC = 7; private Handler mHandler; private TextView mCommand;
private TextView mStatus;
private Button mRecord;
private Button mRedo;
private RadioButton m8KHz;
private RadioButton m11KHz;
private RadioButton mCall;
private RadioButton mDialNanp;
private RadioButton mDialPairs; private InputStream mMicrophone;
private ByteArrayOutputStream mBaos; private File mUtterance;
private int mSampleRate;
private Thread mThread;
private boolean mStoppedListening; @Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle); mHandler = new Handler(); setContentView(R.layout.recorder);
mCommand = (TextView) findViewById(R.id.commandText);
mStatus = (TextView) findViewById(R.id.statusText);
mRecord = (Button) findViewById(R.id.recordButton);
mRedo = (Button) findViewById(R.id.redoButton);
m8KHz = (RadioButton)findViewById(R.id.codec8KHzRadioButton);
m11KHz = (RadioButton)findViewById(R.id.codec11KHzRadioButton);
mCall = (RadioButton)findViewById(R.id.callRadioButton);
mDialNanp = (RadioButton)findViewById(R.id.dialNanpRadioButton);
mDialPairs = (RadioButton)findViewById(R.id.dialPairsRadioButton); mCommand.setText("Please click 'Record' to begin");
mRecord.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (Config.LOGD) {
Log.d(TAG, "mRecord.OnClickListener.onClick");
} setupRecording();
}
}); mRedo.setEnabled(false);
mRedo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (Config.LOGD) {
Log.d(TAG, "mRedo.onClickListener.onClick");
} mUtterance.delete(); setupRecording();
}
}); m8KHz.setText("PCM/16bit/8KHz");
m11KHz.setText("PCM/16bit/11KHz");
m11KHz.setChecked(true);
mCall.setChecked(true);
} private void setupRecording() {
Log.d(TAG, "setupRecording");
// disable buttons
mRedo.setEnabled(false);
mRecord.setEnabled(false);
m8KHz.setFocusable(false);
m11KHz.setFocusable(false);
mCall.setFocusable(false);
mDialNanp.setFocusable(false);
mDialPairs.setFocusable(false); // find the first utterance not covered
String[] utterances = mCall.isChecked() ? mCallUtterances :
mDialNanp.isChecked() ? mDialNanpUtterances :
mDialPairs.isChecked() ? mDialPairsUtterances :
null;
mUtterance = null;
int index = -1;
for (int i = 0; i < utterances.length; i++) {
File u = new File(getDir("recordings", MODE_PRIVATE),
utterances[i].toLowerCase().replace(' ', '_') + ".wav");
if (!u.exists()) {
mUtterance = u;
index = i;
break;
}
} // check if done
if (mUtterance == null) {
mCommand.setText("Finished: Thank You!");
return;
}
Log.d(TAG, "going to record " + mUtterance.toString()); // fix up UI
mCommand.setText("Say: \"" + utterances[index] + "\"");
final String status = "item " + (index + 1) + "/" + utterances.length; // start the microphone
mSampleRate = m8KHz.isChecked()? 8000 :
m11KHz.isChecked() ? 11025 :
11025;
mBaos = new ByteArrayOutputStream(mSampleRate * 2 * 20);
try {
mMicrophone = new MicrophoneInputStream(mSampleRate, mSampleRate * 15);// mMicrophone = logInputStream(mUtterance.toString(), mMicrophone, mSampleRate);
} catch (IOException e) { } // post a number of delayed events to update the UI and to stop recording
// after a few seconds.
for (int i = 0; i <= DURATION_SEC; i++) {
final int remain = DURATION_SEC - i;
mHandler.postDelayed(new Runnable() {
public void run() {
if (remain > 0) {
mStatus.setText(status + " Recording... " + remain);
}
else {
mStatus.setText(status);
stopRecording();
}
}
}, i * 1000);
} // now start a thread to store the audio.
mStoppedListening = false;
mThread = new Thread() {
public void run() {
Log.d(TAG, "run audio capture thread");
byte buffer[] = new byte[512];
while (!mStoppedListening) {
try {
int rtn = 0;
rtn = mMicrophone.read(buffer, 0, 512);
if (rtn > 0) mBaos.write(buffer, 0, rtn);
} catch (IOException e) {
}
}
}
};
mThread.start(); // to avoid the button click
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
} } private void stopRecording() {
Log.d(TAG, "stopRecording");
mStoppedListening = true;
try {
mThread.join();
} catch (InterruptedException e) { }
try {
OutputStream out = new FileOutputStream(mUtterance.toString());
try {
byte[] pcm = mBaos.toByteArray();
Log.d(TAG, "byteArray length " + pcm.length);
WaveHeader hdr = new WaveHeader(WaveHeader.FORMAT_PCM,
(short)1, mSampleRate, (short)16, pcm.length);
hdr.write(out);
out.write(pcm);
} finally {
out.close();
mMicrophone.close();
mBaos.close();
}
} catch (IOException e) {
} finally {
} // stop the recording
mRecord.setEnabled(true); mRedo.setEnabled(true); mCommand.setText("Got it!");
}
private final static String[] mCallUtterances = new String[] {
"Call Adam Varro",
"Call Alex Lloyd",
}; private final static String[] mDialPairsUtterances = new String[] {
// all possible pairs
"Dial 000 000 0000",
"Dial 101 010 1010",
};
private final static String[] mDialNanpUtterances = new String[] {
"Dial 211",
"Dial 411",
};
}
utterances[i].toLowerCase().replace(' ', '_') + ".wav");
找不到这个目录, 这个文件.
File u = new File("/sdcard/",
utterances[i].toLowerCase().replace(' ', '_') + ".wav");