所有,我正在使用 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();
我还尝试了 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();
}
}
让我知道任何需要的细节。谢谢。
最佳答案
每个音频文件都有自己的标题(包括有关长度/样本等的信息) - 通过按照您的方式组合文件,生成的文件有多个标题,每个源文件一个(取决于确切的格式和文件偏移等) .). 因此,生成的文件在文件格式规范方面是不正确的。
较新的 Android 版本更加宽松,并且可以在存在“多个 header ”的情况下工作/玩耍……旧版本不会……
要创建正确组合的音频文件,您必须符合规范,其中包括创建一个描述所有包含音频的新 header ...
使用不同的方法组合音频文件 - 例如通过 ffmpeg(有关如何为 android 制作 ffmpeg,请参阅 this)。
关于android - 合并音频文件并在不同的 API 版本中播放时出现奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8268195/