Mah*_*iya 2 media audio file-io android
我MediaRecorder在我的Android应用程序中使用.我参考public void setOutputFile (String path)了MediaRecorder该类的在线文档,但无法找到有关它是否覆盖现有文件在同一路径上还是附加到现有文件(如果存在)的信息.
那么,有两个问题:
public void setOutputFile (String path)覆盖文件path,如果它存在,或者它附加到文件path?提前致谢.
到目前为止,我发现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();
Run Code Online (Sandbox Code Playgroud)
我的两个音频文件组合代码如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
简而言之,我正在做的是 - 跳过第二个文件的标题(AMR文件中标题的长度是6个字节).
这对我有用,我在Android 2.3版上进行了测试
希望这有助于他人!