合并音频文件并在不同 API 版本中播放时出现奇怪的问题
所有,我正在使用媒体录音机来录制音频。
案例 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();
}
}
让我知道需要的任何详细信息。谢谢。
All , I am using Media Recorder for Recording Audio.
Case 1: If i use Android Version 2.2 installed devices, my recorded audio combined and playing well.
Case 2: If i use it in Android 1.6 installed devices, i am not able to play the combined audio file.
It is playing only the very first recorded audio and next recorded audio files keep empty no sound.
Also i am not getting any error in Logcat.
I used following code for recording audio :
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();
Also i tried for mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
Code for combining Audio file :
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();
}
}
Let me know any details need.Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个音频文件都有自己的标头(包括有关长度/样本等的信息) - 通过按照您的方式组合文件,生成的文件具有多个标头,每个源文件一个标头(取决于带有文件偏移量等的确切格式)。
因此,生成的文件在文件格式规范方面不正确。
较新的 Android 版本更加宽松,并且可以使用“多个标头”进行工作/播放...旧版本则不...
要创建正确组合的音频文件,您必须符合规范,这意味着创建一个新标头它描述了所有包含的音频...
使用不同的方法组合音频文件 - 例如通过 ffmpeg (请参阅此 制作方法适用于 Android 的 ffmpeg)。
Every audio file has its own header (includes information about length/samples etc.) - by combining the files the way you do the resulting file has multiple headers, one per source file (depending on the exact format with file offsets etc.).
Thus the resulting file is NOT correct in terms of file format spec.
The newer Android version are more permissive and work/play with "multiple headers" present... the older versions do not...
To create a correctly combined audio file you must conform to the spec which among other things means creating one new header which describes all included audio...
Use for the the combination of audio files a different approach - for example via ffmpeg (see this for how to make ffmpeg for android).
前言:还没有测试过这个,但我不明白为什么它不起作用。
如果标头是导致此问题的原因,那么您可以很轻松地解决它。使用您提供的代码,编码是 AMR-NB。根据 本文档,AMR 标头只是第一个6个字节,分别是0x23、0x21、0x41、0x4D、0x52、0x0A。如果后续文件中的标头导致问题,只需从后续文件中省略这些字节即可,例如
让我知道情况如何。
编辑:根据要求,将 try 块更改为:
Foreword: Haven't tested this, but I don't see why it shouldn't work.
Provided the headers ARE the cause of this problem, you can solve it really easily. Using the code you've given, the encoding is AMR-NB. According to this document the AMR header is simply the first 6 bytes, which are 0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A. If the headers in subsequent files are causing the issue, simply omit those bytes from subsequent files e.g.
Let me know how it goes.
EDIT: At request, change the try block to: