Android MediaRecorder类:setOutputFile()方法是否将数据附加到现有文件?
我正在Android应用程序中使用Android MediaRecorder类:setOutputFile()方法是否将数据附加到现有文件?,android,audio,file-io,media,Android,Audio,File Io,Media,我正在Android应用程序中使用MediaRecorder。我参考了MediaRecorder类的public void setOutputFile(String path)的在线文档,但找不到关于它是否覆盖同一路径的现有文件或附加到现有文件(如果存在)的信息 因此,有两个问题: public void setOutputFile(字符串路径)是否覆盖路径处的文件(如果存在),或将其附加到路径处的文件 有没有办法恢复保存在特定路径上的已暂停录制 提前感谢。从我的发现来看,我的结论是Androi
MediaRecorder
。我参考了MediaRecorder
类的public void setOutputFile(String path)
的在线文档,但找不到关于它是否覆盖同一路径的现有文件或附加到现有文件(如果存在)的信息
因此,有两个问题:
public void setOutputFile(字符串路径)
是否覆盖路径
处的文件(如果存在),或将其附加到路径
处的文件提前感谢。从我的发现来看,我的结论是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;
}
受保护的Void doInBackground(字符串…文件路径){
试一试{
字符串originalVoiceMessageRecording=filepath[0];
字符串newVoiceMessageRecording=文件路径[1];
File outputFile=新文件(originalVoiceMessageRecording);
FileOutputStream fos=新的FileOutputStream(outputFile,true);//第二个参数,用于指示附加数据
文件输入文件=新文件(newVoiceMessageRecording);
FileInputStream fis=新的FileInputStream(inputFile);
Log.d(Log_标记,“outputFile的长度:“+outputFile.Length()+”和inputFile的长度:“+inputFile.Length()”);
byte fileContent[]=新字节[(int)inputFile.length()];
read(fileContent);//从列表中以字节形式读取文件内容。
/*复制整个文件,但不复制前6个字节*/
byte[]headerlessFileContent=新字节[fileContent.length-6];
对于(int j=6;jThanks,节省了我很多时间!所有其他帖子都说只需连接每个文件的字节数组,但完全忽略了忽略第二个文件的头。你能用THREE_GP或AAC_ADTS格式做同样的事吗?它必须是原始的.WAV文件吗?它能生成WAV吗