从我的发现来看,我得出的结论是,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 版上对其进行了测试
希望这对其他人有所帮助!