![](/img/trans.png)
[英]Is the file recorded with android AudioRecorder class saved somewhere?
[英]Recorded Audio Can't be saved in /storage/emulated/0/AudioRecorder (sometimes saved file appear, sometimes not appear)
我想將錄制的音頻保存在/ storage / emulated / 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);
}
}
該文件有時出現在/ storage / emulated / 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();
}
}
因此,當文件存在時,不需要再次創建該文件,但是當保存的文件不存在時,則需要創建該文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.