我要救我的录音在/存储/模拟/ 0 / AudioRecorder,这是我的代码:
private String getFilename() {
File file = new File(Environment.getExternalStorageDirectory(), "/AudioRecorder");
if (!file.exists()) {
file.mkdirs();
}
createdDate = DateTools.getDate(System.currentTimeMillis(),"dd-MMM-yyyy hh:mm:ss");
nameFile = createdDate + ".mp4";
pathFile = (file.getAbsolutePath() + "/" + nameFile);
return pathFile;
}
private void startRecording() {
if(RadioManager.getInstance().isPlaying()){
RadioManager.getInstance().stopPlayer();
}
Toast.makeText(VoiceNoteActivity.this, "Mulai Rekam", Toast.LENGTH_SHORT).show();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
isRecord = true;
countDown.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
if (null != recorder) {
countDown.cancel();
timer = 0;
seekbar.setProgress(timer);
try{
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}catch(RuntimeException stopException){
//handle cleanup here
Log.e("Voice Note Activity ","Voice Recording Error : " +stopException.getMessage());
}
isRecord = false;
time.setText("00:00");
enableButtons(false);
}
}
文件有时会出现在/存储/模拟/ 0 / AudioRecorder,但有时不显示(更经常地不会出现)。任何人都可以帮忙吗?
哼哼..我有同样的问题,但与图片不的声音。在我的情况下,画面没有出现,因为我还没有扔给画廊。基于Android开发者网站上的教程是它扔图片库中的代码:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(fileUri.getPath());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
可能是你可以用它来语音文件。但我还没有尝试语音文件。
我已经通过添加以下代码解决了这个:
File file = new File(getFilename());
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
所以,当该文件是存在的,它并不需要重新创建该文件,但如果保存的文件不存在,则需要创建该文件。