【问题标题】:Android MediaRecorder Class: Does the setOutputFile() method append data to the existing file?Android MediaRecorder 类:setOutputFile() 方法是否将数据附加到现有文件?
【发布时间】:2012-03-28 05:47:14
【问题描述】:

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

那么,两个问题:

  1. public void setOutputFile (String path) 是否覆盖path 处的文件(如果存在),还是附加到path 处的文件?
  2. 有没有办法恢复保存在特定路径的已暂停录制?

提前致谢。

【问题讨论】:

    标签: android audio file-io media


    【解决方案1】:

    从我的发现来看,我得出的结论是,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 版上对其进行了测试

    希望这对其他人有所帮助!

    【讨论】:

    • 谢谢,为我节省了很多时间!所有其他帖子都说只是连接每个文件中的字节数组,但完全忽略了第二个文件的标题。
    • 你能对 THREE_GP 或 AAC_ADTS 格式做同样的事情吗?它必须是原始的 .WAV 文件吗?
    • 它会发出 wav
    猜你喜欢
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多