[英]How to save an audio recorded in internal storage
我正在開發的 android 應用程序; 它有一個錄音選項; 我希望它在設備的內部存儲中保存一個新的音頻錄制文件,這樣用戶和其他應用程序都無法訪問這些錄制的音頻文件,除非他們打開我的應用程序。
我的主要困難是能夠將該音頻文件保存在內部存儲中:我花時間在我的 android 編程書籍中進行了復習,並在這里閱讀了一些問題和答案: 如何在內部存儲中保存音頻文件 android和https:// developer.android.com/guide/topics/data/data-storage#filesInternal和https://www.tutorialspoint.com/android/android_internal_storage.htm但不幸的是我從那里得到的東西根本沒有解決我的問題。
我用來記錄的代碼如下:
private MediaRecorder rec;
private String file_folder="/scholar/";
String
file_path=Environment.getExternalStorageDirectory().getPath();
File file= new File(file_path,file_folder);
Long date=new Date().getTime();
Date current_time = new Date(Long.valueOf(date));
rec=new MediaRecorder();
rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
rec.setAudioChannels(1);
rec.setAudioSamplingRate(8000);
rec.setAudioEncodingBitRate(44100);
rec.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB);
rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (!file.exists()){
file.mkdirs();
}
/*the file here this one:File file= new File(file_path,file_folder);
it means the value of the outputfile is the one which will be provided by
rec.setOutputFile() method, so it will be saved as this name:
file.getAbsolutePath()+"/"+"_"+current_time+".amr"
*/
rec.setOutputFile(file.getAbsolutePath()+"/"+"_"+current_time+".amr");
try {
rec.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Recording_Service.this,"Sorry! file creation failed!",Toast.LENGTH_SHORT).show();
return;
}
rec.start();
rec.stop();
rec.reset();
不幸的是,我在那里做的是將我的文件保存在外部存儲上,這意味着錄制后,該文件對設備文件資源管理器中的每個人都可見,他們甚至可以刪除該文件。
所以! 拜托,我需要你們的幫助,如果有任何幫助,我將不勝感激。 謝謝!
我找到了這個問題的答案,我使用了getFilesDir()
;
private MediaRecorder rec;
String file_path=getApplicationContext().getFilesDir().getPath();
File file= new File(file_path);
Long date=new Date().getTime();
Date current_time = new Date(Long.valueOf(date));
rec=new MediaRecorder();
rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
rec.setAudioChannels(1);
rec.setAudioSamplingRate(8000);
rec.setAudioEncodingBitRate(44100);
rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (!file.exists()){
file.mkdirs();
}
String file_name=file+"/"+current_time+".3gp";
rec.setOutputFile(file_name);
try {
rec.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Recording_Service.this,"Sorry! file creation failed!"+e.getMessage(),Toast.LENGTH_SHORT).show();
return;
}
rec.start();
rec.stop();
rec.reset();
感謝所有試圖幫助我回答的人。 讓我們繼續享受編碼的樂趣,讓我們的世界更加甜蜜愉快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.