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

前端 未结 4 2004
-上瘾入骨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:12

    I also struggled with that and solved it using 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;
    

    For your case, I believe something like this should work:

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

    There are a few answers suggesting that, such as:

    • Merging 2 audio files sequentially in android?
    • Concatenate two audio files and play resulting file
    0 讨论(0)
  • 2020-12-09 14:24

    It is too late. But still, someone might need a proper solution. That is why I am suggesting using AudioMixer-android library.

    0 讨论(0)
  • 2020-12-09 14:27

    String wavFile1 = android.os.Environment.getExternalStorageDirectory()+"/Download/a.mp4"; String wavFile2 = android.os.Environment.getExternalStorageDirectory()+"/Download/b.mp4"; FileInputStream fistream1 = null; // first source file try { fistream1 = new FileInputStream(wavFile1); FileInputStream fistream2 = new FileInputStream(wavFile2);//second source file SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2); FileOutputStream fostream = new FileOutputStream(android.os.Environment.getExternalStorageDirectory()+"/Download/merge1.mp4");//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
            }
            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();
        }
    
    0 讨论(0)
  • 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<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;
            }
        }
    
    • 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
    0 讨论(0)
提交回复
热议问题