如何在android中合并两个或多个mp3音频文件?

时间:2016-02-11 12:59:41

标签: android audio merge mp3

我正在尝试合并mp3音频文件但不成功。

这是我的代码。

public static void meargeAudio(List<File> filesToMearge)
{


    while (filesToMearge.size()!=1){

        try {
            FileInputStream fistream1 = new FileInputStream(new File(filesToMearge.get(0).getPath()));  //(/storage/emulated/0/Audio Notes/1455194356500.mp3) first source file
            FileInputStream fistream2 = new FileInputStream(new File(filesToMearge.get(1).getPath()));//second source file

            File file1 = new File(filesToMearge.get(0).getPath());
            boolean deleted = file1.delete();
            File file2 = new File(filesToMearge.get(1).getPath());
            boolean deleted1 = file2.delete();

            SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
            FileOutputStream fostream = new FileOutputStream(new File(filesToMearge.get(0).getPath()),true);//destinationfile

            int temp;

            while ((temp = sistream.read()) != -1) {
                // System.out.print( (char) temp ); // to print at DOS prompt
                fostream.write(temp);   // to write to file
            }

            filesToMearge.add(0,new File(filesToMearge.get(0).getPath()));
            filesToMearge.remove(1);
            filesToMearge.remove(1);


            fostream.close();
            sistream.close();
            fistream1.close();
            fistream2.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

例如

firstFileSize = 12kb

secondFileSize = 10kb

finalfileSize = 22KB

尺寸准确但缺少声音

没有错误但是在结果中我发现finalfile只包含第一个文件音频第二个文件音频丢失。

不知道是什么问题。任何人都知道解决方案可以帮助我。

谢谢

4 个答案:

答案 0 :(得分:3)

我也很努力,并使用mp4parser

解决了这个问题
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;
import com.googlecode.mp4parser.authoring.tracks.AppendTrack;

对于你的情况,我相信这样的事情应该有效:

public static void mergeAudio(List<File> filesToMerge) {

    String output = Environment.getExternalStorageDirectory().getAbsolutePath() + "output.mp3";

    while (filesToMerge.size()!=1){

        try {

            String[] videoUris = new String[]{
                filesToMerge.get(0).getPath(),
                filesToMerge.get(0).getPath()
            };

            List<Track> videoTracks = new LinkedList<Track>();
            List<Track> audioTracks = new LinkedList<Track>();

            for (Movie m : inMovies) {
                for (Track t : m.getTracks()) {
                    if (t.getHandler().equals("soun")) {
                        audioTracks.add(t);
                    }
                    if (t.getHandler().equals("vide")) {
                        videoTracks.add(t);
                    }
                }
            }

            Movie result = new Movie();

            if (!audioTracks.isEmpty()) {
                result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
            }
            if (!videoTracks.isEmpty()) {
                result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
            }

            Container out = new DefaultMp4Builder().build(result);

            FileChannel fc = new RandomAccessFile(output, "rw").getChannel();
            out.writeContainer(fc);
            fc.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

有一些答案表明,例如:

答案 1 :(得分:0)

有合并音频或视频文件的功能

public static boolean mergeMediaFiles(boolean isAudio, String sourceFiles[], String targetFile) {
        try {
            String mediaKey = isAudio ? "soun" : "vide";
            List<Movie> listMovies = new ArrayList<>();
            for (String filename : sourceFiles) {
                listMovies.add(MovieCreator.build(filename));
            }
            List<Track> listTracks = new LinkedList<>();
            for (Movie movie : listMovies) {
                for (Track track : movie.getTracks()) {
                    if (track.getHandler().equals(mediaKey)) {
                        listTracks.add(track);
                    }
                }
            }
            Movie outputMovie = new Movie();
            if (!listTracks.isEmpty()) {
                outputMovie.addTrack(new AppendTrack(listTracks.toArray(new Track[listTracks.size()])));
            }
            Container container = new DefaultMp4Builder().build(outputMovie);
            FileChannel fileChannel = new RandomAccessFile(String.format(targetFile), "rws").getChannel();
            container.writeContainer(fileChannel);
            fileChannel.close();
            return true;
        }
        catch (IOException e) {
            Log.e("MYTAG", "Error merging media files. exception: "+e.getMessage());
            return false;
        }
    }
  • 如果您有Audios,isAudio应该是真的。如果你有视频isAudio应该是假的
  • sourceFiles[]数组应包含媒体文件的目标,例如

    /sdcard/my_songs/audio_1, /sdcard/my_songs/audio_2, /sdcard/my_songs/audio_3

  • targetFile是您的最终音频合并文件应该是

    /sdcard/my_songs/final_audio_1

  • 如果你处理大文件,最好在后台合并文件,你可以使用AsynkTask

答案 2 :(得分:0)

为时已晚。但是,仍然有人可能需要适当的解决方案。这就是为什么我建议使用AudioMixer-android库的原因。

答案 3 :(得分:0)

字符串wavFile1 = android.os.Environment.getExternalStorageDirectory()+“ / Download / a.mp4”; 字符串wavFile2 = android.os.Environment.getExternalStorageDirectory()+“ / Download / b.mp4”; FileInputStream fistream1 = null; //第一个源文件 尝试{ fistream1 =新的FileInputStream(wavFile1); FileInputStream fistream2 =新的FileInputStream(wavFile2); //第二个源文件 SequenceInputStream sistream =新的SequenceInputStream(fistream1,fistream2); FileOutputStream fostream =新的FileOutputStream(android.os.Environment.getExternalStorageDirectory()+“ / Download / merge1.mp4”); //目标文件

        int temp;

        while( ( temp = sistream.read() ) != -1)
        {
            // System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write(temp);   // to write to file
        }
        Log.e("Result","Done");

        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();

        Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.e("Not",e+"");
        Toast.makeText(this, "File not Found", Toast.LENGTH_SHORT).show();
    }
    catch (IOException e)
    {
        Toast.makeText(this, ""+e, Toast.LENGTH_SHORT).show();
    }