问题描述
我需要录制语音这个函数 setoutputFile() 需要一个输出文件的路径并且需要处理我的 Uri 不是路径的作用域存储我该怎么办?
<select>
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
<option value="Option3">Option3</option>
<option value="Option4">Option4</option>
</select>
这一行应该改变
public void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setoutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recordingName = getFilename();
recorder.setoutputFile(recordingName);
recorder.setonErrorListener(errorListener);
recorder.setonInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
handler.post(updateVisualizer);
} catch (IllegalStateException | IOException e) {
e.printstacktrace();
}
}
解决方法
我为我的应用使用了公共文件夹 像这个例子 https://github.com/CWMChapman/Audio_Memos
public void startRecording(Button b) {
// state = mounted or unmounted
String state = Environment.getExternalStorageState();
Log.d(TAG,state);
Context ctx = this.getApplicationContext();
File audioDir = new File(ctx.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),"AudioMemos");
audioDir.mkdirs();
String audioDirPath = audioDir.getAbsolutePath();
Log.d(TAG,"Recording file location: " + audioDirPath);
Date currentTime = Calendar.getInstance().getTime(); // current time
String curTimeStr = currentTime.toString().replace(" ","_");
File recordingFile = new File(audioDirPath + "/" + curTimeStr + ".m4a");
Log.d(TAG,"Created file: " + recordingFile.getName());
// try {
// } catch (IOException e) {
// Log.e(TAG,"external storage access error");
// return;
// }
mr = new MediaRecorder();
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mr.setOutputFile(recordingFile.getAbsolutePath());
try {
mr.prepare();
} catch (IOException e) {
Log.e(TAG,"prepare() failed");
}
mr.start();
}