我真的面临着最后几天的问题,但我不能找到确切的解决方案,请帮助我。
我想合并两个mp3或者任何音频文件并播放最后的单一的mp3文件。但是,当我合并两个文件的最终文件大小是确定的,但,当我试图发挥它只是起到第一个文件,我曾试图与的SequenceInputStream
或字节数组,但我我无法得到确切的结果,请帮助我。
我的code是如下:
公共类MerginFileHere延伸活动{
公众的ArrayList<字符串> audNames;
字节fileContent [];
字节fileContent1 [];
的FileInputStream插件,INS1;
FileOutputStream中FOS = NULL;
字符串combined_file_stored_path =环境
.getExternalStorageDirectory()。getPath()
+/AudioRecorder/final.mp3;
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
audNames =新的ArrayList<字符串>();
字符串文件1 = Environment.getExternalStorageDirectory()。getPath()
+/AudioRecorder/one.mp3;
字符串文件2 = Environment.getExternalStorageDirectory()。getPath()
+/AudioRecorder/two.mp3;
档案文件=新的文件(Environment.getExternalStorageDirectory()
.getPath()+/ AudioRecorder /+final.mp3);
尝试 {
file.createNewFile();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
audNames.add(文件1);
audNames.add(文件2);
按钮BTN =(按钮)findViewById(R.id.clickme);
btn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
createCombineRecFile();
}
});
}
公共无效createCombineRecFile(){
存储在字符串//字符串combined_file_stored_path = //文件路径
//录制的音频
尝试 {
FOS =新的FileOutputStream(combined_file_stored_path,真正的);
}赶上(FileNotFoundException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
尝试 {
文件F =新的文件(audNames.get(0));
文件F1 =新的文件(audNames.get(1));
Log.i(记录信息,文件长度=========>>>+ f.length()+-------------&GT ;+ f1.length());
fileContent =新的字节[(INT)f.length()];
插件=新的FileInputStream(audNames.get(0));
INT R = ins.read(fileContent); //读取文件内容为字节
fileContent1 =新的字节[(INT)f1.length()];
INS1 =新的FileInputStream(audNames.get(1));
INT R1 = ins1.read(fileContent1); //读取文件内容为字节
//从列表。
Log.i(记录信息,字节数Readed =====>>>+ R);
//fos.write(fileContent1);//写字节到合并文件中。
byte []的组合=新的字节[fileContent.length + fileContent1.length]。
的for(int i = 0; I< combined.length ++ I)
{
结合[I] = I< fileContent.length? fileContent [我]:fileContent1 [我 - fileContent.length]。
}
fos.write(组合);
//fos.write(fileContent1);*
}赶上(FileNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
尝试 {
fos.close();
Log.v(记录信息,=====合并文件关闭=====);
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
解决方案
我已经出版了此功能的应用程序...试试我的方法使用的SequenceInputStream,在我的应用程序,我只是合并在一个17的MP3文件,并通过播放JNI库MPG123,但我测试用MediaPlayer的没有问题的文件。
这code是不是最好的,但它的工作原理...
私人无效mergeSongs(文件mergedFile,文件... mp3Files){
的FileInputStream fisToFinal = NULL;
FileOutputStream中FOS = NULL;
尝试 {
FOS =新的FileOutputStream(mergedFile);
fisToFinal =新的FileInputStream(mergedFile);
对于(文件mp3File:mp3Files){
如果(!mp3File.exists())
继续;
的FileInputStream fisSong =新的FileInputStream(mp3File);
的SequenceInputStream SIS =新的SequenceInputStream(fisToFinal,fisSong);
byte []的BUF =新的字节[1024];
尝试 {
对于(INT readNum;(readNum = fisSong.read(BUF))= -1;!)
fos.write(BUF,0,readNum);
} 最后 {
如果(fisSong!= NULL){
fisSong.close();
}
如果(SIS!= NULL){
sis.close();
}
}
}
}赶上(IOException异常E){
e.printStackTrace();
}最后{
尝试 {
如果(FOS!= NULL){
fos.flush();
fos.close();
}
如果(fisToFinal!= NULL){
fisToFinal.close();
}
}赶上(IOException异常E){
e.printStackTrace();
}
}
}
I am really facing problem from last couple of days but I am not able to find the exact solution please help me.
I want to merge two .mp3 or any audio file and play final single one mp3 file. But when I am combine two file the final file size is ok but when I am trying to play it just play first file, I have tried this with SequenceInputStream
or byte array but I am not able to get exact result please help me.
My code is the following:
public class MerginFileHere extends Activity {
public ArrayList<String> audNames;
byte fileContent[];
byte fileContent1[];
FileInputStream ins,ins1;
FileOutputStream fos = null;
String combined_file_stored_path = Environment
.getExternalStorageDirectory().getPath()
+ "/AudioRecorder/final.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audNames = new ArrayList<String>();
String file1 = Environment.getExternalStorageDirectory().getPath()
+ "/AudioRecorder/one.mp3";
String file2 = Environment.getExternalStorageDirectory().getPath()
+ "/AudioRecorder/two.mp3";
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/AudioRecorder/" + "final.mp3");
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
audNames.add(file1);
audNames.add(file2);
Button btn = (Button) findViewById(R.id.clickme);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
createCombineRecFile();
}
});
}
public void createCombineRecFile() {
// String combined_file_stored_path = // File path in String to store
// recorded audio
try {
fos = new FileOutputStream(combined_file_stored_path, true);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
File f = new File(audNames.get(0));
File f1 = new File(audNames.get(1));
Log.i("Record Message", "File Length=========>>>" + f.length()+"------------->"+f1.length());
fileContent = new byte[(int) f.length()];
ins = new FileInputStream(audNames.get(0));
int r = ins.read(fileContent);// Reads the file content as byte
fileContent1 = new byte[(int) f1.length()];
ins1 = new FileInputStream(audNames.get(1));
int r1 = ins1.read(fileContent1);// Reads the file content as byte
// from the list.
Log.i("Record Message", "Number Of Bytes Readed=====>>>" + r);
//fos.write(fileContent1);// Write the byte into the combine file.
byte[] combined = new byte[fileContent.length + fileContent1.length];
for (int i = 0; i < combined.length; ++i)
{
combined[i] = i < fileContent.length ? fileContent[i] : fileContent1[i - fileContent.length];
}
fos.write(combined);
//fos.write(fileContent1);*
} 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();
}
}
}
解决方案
I already published an app with this function... try my method using SequenceInputStream, in my app I just merge 17 MP3 files in one and play it using the JNI Library MPG123, but I tested the file using MediaPlayer without problems.
This code isn't the best, but it works...
private void mergeSongs(File mergedFile,File...mp3Files){
FileInputStream fisToFinal = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mergedFile);
fisToFinal = new FileInputStream(mergedFile);
for(File mp3File:mp3Files){
if(!mp3File.exists())
continue;
FileInputStream fisSong = new FileInputStream(mp3File);
SequenceInputStream sis = new SequenceInputStream(fisToFinal, fisSong);
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fisSong.read(buf)) != -1;)
fos.write(buf, 0, readNum);
} finally {
if(fisSong!=null){
fisSong.close();
}
if(sis!=null){
sis.close();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos!=null){
fos.flush();
fos.close();
}
if(fisToFinal!=null){
fisToFinal.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}