gpt4 book ai didi

Android MediaRecorder 类 : Does the setOutputFile() method append data to the existing file?

转载 作者:太空狗 更新时间:2023-10-29 16:22:53 26 4
gpt4 key购买 nike

我在我的 Android 应用程序中使用 MediaRecorder。我引用了 MediaRecorder 类的 public void setOutputFile (String path) 的在线文档,但找不到有关它是否同时覆盖现有文件的信息路径或附加到现有文件(如果存在)。

那么,两个问题:

  1. public void setOutputFile (String path) 是否覆盖 path 中的文件(如果存在),或者附加到 path 中的文件?
  2. 有什么方法可以恢复保存在特定路径的已经暂停的录音吗?

提前致谢。

最佳答案

根据我的发现,我得出的结论是 Android API 目前不提供使用 MediaRecorder 恢复录制的选项。

为了实现合并两个录制音频的目标,我通过跳过第二个文件的标题直接合并了两个文件。

以下是我的引用代码:

    mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);

/* If the voiceMessage in consideration is a PAUSED one, then we should
* record at a new alternate path so that earlier recording does not get overwritten, and can be used later for merging */
Log.d(LOG_TAG, "VoiceMessageManager:recordVoiceMessage() - state of voice message - " + voiceMessage.getVoiceMessageState());

mRecorder.setOutputFile(filePath);

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.prepare();
mRecorder.start();

我合并两个音频文件的代码如下:

protected Void doInBackground(String... filePaths) {
try {
String originalVoiceMessageRecording = filePaths[0];
String newVoiceMessageRecording = filePaths[1];

File outputFile = new File(originalVoiceMessageRecording);
FileOutputStream fos = new FileOutputStream(outputFile, true); // Second parameter to indicate appending of data

File inputFile = new File(newVoiceMessageRecording);
FileInputStream fis = new FileInputStream(inputFile);

Log.d(LOG_TAG, "Length of outputFile: " + outputFile.length() + " and Length of inputFile: " + inputFile.length() );

byte fileContent[]= new byte[(int)inputFile.length()];
fis.read(fileContent);// Reads the file content as byte from the list.

/* copy the entire file, but not the first 6 bytes */
byte[] headerlessFileContent = new byte[fileContent.length-6];
for(int j=6; j<fileContent.length;j++){
headerlessFileContent[j-6] = fileContent[j];
}
fileContent = headerlessFileContent;

/* Write the byte into the combine file. */
fos.write(fileContent);

/* Delete the new recording as it is no longer required (Save memory!!!) :-) */
File file = new File(newVoiceMessageRecording);
boolean deleted = file.delete();

Log.d(LOG_TAG, "New recording deleted after merging: " + deleted);
Log.d(LOG_TAG, "Successfully merged the two Voice Message Recordings");
Log.d(LOG_TAG, "Length of outputFile after merging: " + outputFile.length());
} catch (Exception ex) {
Log.e(LOG_TAG, "Error while merging audio file: " + ex.getMessage());
}
return null;
}

简而言之,我正在做的是 - 跳过第二个文件的标题(AMR 文件中的标题长度为 6 个字节)。

这对我有用,我在 Android 2.3 版上测试过

希望这对其他人有帮助!

关于Android MediaRecorder 类 : Does the setOutputFile() method append data to the existing file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901786/

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