组合音频文件和在不同API版本中播放的奇怪问题

Ven*_*nky 10 media audio android version

全部,我正在使用Media Recorder录制音频.

案例1:如果我使用Android 2.2版安装的设备,我录制的音频组合并播放得很好.

案例2:如果我在Android 1.6安装的设备中使用它,我无法播放组合的音频文件.

它只播放第一个录制的音频,下一个录制的音频文件保持空白没有声音.

此外,我没有在Logcat中收到任何错误.

我使用以下代码录制音频:

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
    mRecorder.setOutputFile(main_record_file);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mRecorder.prepare();
    mRecorder.start();
Run Code Online (Sandbox Code Playgroud)

我也试过 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

组合音频文件的代码:

    public void createCombineRecFile(){
    combined_file_stored_path=getFilename_combined_raw(); // File path in String to store recorded audio
    byte fileContent[]=null;
    FileInputStream ins;
    FileOutputStream fos = null;
    try{
        fos = new FileOutputStream(combined_file_stored_path,true);
    }
    catch (FileNotFoundException e1){
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    for(int i=0;i<audNames.size();i++){
        try{
            File f=new File(audNames.get(i));
            Log.v("Record Message", "File Length=========>>>"+f.length());
            fileContent = new byte[(int)f.length()];
            ins=new FileInputStream(audNames.get(i));
            int r=ins.read(fileContent);// Reads the file content as byte from the list.
            Log.v("Record Message", "Number Of Bytes Readed=====>>>"+r);
            fos.write(fileContent);//Write the byte into the combine file.

            Log.v("Record Message", "File======="+i+"is Appended");

        }
        catch (FileNotFoundException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try{
        fos.close();
        Log.v("Record Message", "===== Combine File Closed =====");
    }
    catch (IOException e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

让我知道任何细节需要.谢谢.

Yah*_*hia 3

每个音频文件都有自己的标头(包括有关长度/样本等的信息) - 通过按照您的方式组合文件,生成的文件具有多个标头,每个源文件一个标头(取决于带有文件偏移量等的确切格式)。因此,生成的文件在文件格式规范方面不正确。

较新的 Android 版本更加宽松,并且可以在“多个标头”的情况下工作/播放...旧版本则不...

要创建正确组合的音频文件,您必须符合规范,这意味着创建一个新的标头来描述所有包含的音频...

使用不同的方法组合音频文件 - 例如通过 ffmpeg(有关如何为 Android 制作 ffmpeg,请参阅此)。