gpt4 book ai didi

java - 如何重命名 mediarecorder 中使用的临时文件?

转载 作者:行者123 更新时间:2023-11-30 11:25:36 25 4
gpt4 key购买 nike

我正在使用 MediaRecorder 构建一个录音机,我想重命名创建的文件(因为 createTempFile 向文件名添加了一个长随机数)

所以我的主要步骤是:

    // I get the files on the directory I'm writing and the amount of files + 1
File f = new File(myDirectory.toString());
File file[] = f.listFiles();
String fileNumber = String.valueOf(file.length+1);

try {
// I create the temp file to record
audiofile = File.createTempFile("record-"+fileNumber, ".3gp", myDirectory);

} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
// START THE RECORDER
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();

当用户单击停止按钮时,我将文件添加到目录中

    values.put(MediaStore.Audio.Media.TITLE, audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();

Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);


sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Successfully added file in " + recordedDirectory+audiofile.getName(), Toast.LENGTH_LONG).show();

问题是文件被命名为例如“记录 3824925563.3gp”

有没有办法在保存之前重命名 TempFile 或阻止 createTempFile() 添加随机数?或者任何其他解决方案来实现这一目标?

我尝试创建一个新文件,然后将 .renameTo(newFile) 强制转换为我的音频文件,但这没有用。

编辑:正如 Michael 所建议的,我尝试使用我想要的参数创建一个新文件并重命名它

File AudioFile = new File(myDirectory+"/record-"+fileNumber+".3gp");

AudioFile.createNewFile();

audiofile.renameTo(AudioFile);

这不会引发任何错误。但是文件最终附加了相同的随机数

编辑 2:

根据 jboi 的回答,效果很好,我添加了我使用的最终代码。我创建了该文件以在我的最终 MediaStore 函数中引用它。

        File f = new File(myDirectory.toString());        
File file[] = f.listFiles();
int fileNumber = file.length+1;

audiofile = new File(String.format(Locale.US, "%s%crecord-%08d.3pg",
quickrecorderDirectory, File.separatorChar, fileNumber));

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());

最佳答案

您可以跳过创建临时文件的步骤。如果在开始录制和停止录制之间发生任何事情(用户取消、设备关闭、应用程序崩溃......),那么您可以稍后在管理任务中整理文件 fragment 和损坏的文件。

所以,我会开始直接录制到最终文件中。该文件将 filenumber+1 作为带前导零的 8 位数字:

// I get the files on the directory I'm writing and the amount of files + 1
File f = new File(myDirectory.toString());
File file[] = f.listFiles();
int fileNumber = file.length+1;

// START THE RECORDER
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(
String.format(Locale.US, "%s%crecord-%08d.3pg",
myDirectory, File.separatorChar, fileNumber));
try {
recorder.prepare();
} catch(IOException e) {
Log.e(TAG, "error while preparing recording", e);
// You might want to inform the user too, with a Toast
return;
}
recorder.start();

停止并通知媒体播放器也很简单:

recorder.stop();
recorder.release();
// Here comes all your code to inform Media scanner

最后你需要一些内务处理,检查目录中的不完整文件并删除它们。它可以在每次启动时运行 onCreate()。要查明文件是否完整,您可能有一个已在使用的数据库。在此数据库中,您可以跟踪所有已知文件名(记录停止时插入)并将文件与数据库进行比较。

关于java - 如何重命名 mediarecorder 中使用的临时文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20229303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com