连接两个音频文件并播放结果文件 [英] Concatenate two audio files and play resulting file

查看:22
本文介绍了连接两个音频文件并播放结果文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天我确实遇到了问题,但我找不到确切的解决方案,请帮助我.

I am really facing problem from last couple of days but I am not able to find the exact solution please help me.

我想合并两个 .mp3 或任何音频文件并播放最后一个 mp3 文件.但是,当我合并两个文件时,最终文件大小还可以,但是当我尝试播放它时,只播放第一个文件,我已经尝试使用 SequenceInputStream 或字节数组进行了此操作,但是我无法获得确切的信息结果请帮帮我.

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.

我的代码如下:

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();
    }
}
}

推荐答案

我已经发布了一个具有此功能的应用程序...尝试使用 SequenceInputStream 的方法,在我的应用程序中,我只是将 17 个 MP3 文件合并为一个文件并使用JNI 库 MPG123,但我使用 MediaPlayer 测试该文件没有问题.

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();
            }
        }
    } 

这篇关于连接两个音频文件并播放结果文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆