简体   繁体   中英

How to mix two audio files in Android and then save؟

I have two audio files in MP3 format inside the user's phone How can I mix these two sounds? That is, I put these two sounds on top of each other and mix them, and then save the final sound in the user's phone I looked everywhere but found nothing

this is my code:

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

I have to finish the project today......help me!

You could use SequenceInputStream. https://docs.oracle.com/javase/8/docs/api/java/io/SequenceInputStream.html

FileInputStream fileInputStream1 = new FileInputStream("/sdcard/mp3/sound1.mp3"); 
FileInputStream fileInputStream2 = new FileInputStream("/sdcard/mp3/sound2.mp3");
SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1, fileInputStream2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM