How to merge two or more mp3 audio file in android?

前端 未结 4 2005
-上瘾入骨i
-上瘾入骨i 2020-12-09 13:53

I\'m trying to merge mp3 audio files But not successful.

Here is my code.

public static void meargeAudio(List filesToMearge)
{


    whi         


        
4条回答
  •  [愿得一人]
    2020-12-09 14:33

    Well there's a function to merge Audio or Video files

    public static boolean mergeMediaFiles(boolean isAudio, String sourceFiles[], String targetFile) {
            try {
                String mediaKey = isAudio ? "soun" : "vide";
                List listMovies = new ArrayList<>();
                for (String filename : sourceFiles) {
                    listMovies.add(MovieCreator.build(filename));
                }
                List 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;
            }
        }
    
    • If you got Audios, isAudio should be true. if you got videos isAudio should be false
    • sourceFiles[] array should contain the destinations of the media files such as

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

    • targetFile is your final audio merged file should be somthing like

      /sdcard/my_songs/final_audio_1

    • If you deal with large files it's preferred to merge file in background, you can use AsynkTask

提交回复
热议问题