6

I'm writing an Android project where I'm recording several audio files. Therefore, I'm setting the following parameters. The recording works fine.

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

My problem is that each time I record, the output is written in a separate file. Now, I need to combine these files to one file. Does anyone have an idea how to combine several MPEG 4 files in an Android project?

Thanks for your help....

user1463683
  • 63
  • 1
  • 4

2 Answers2

8

I would suggest using my mp4parser library then you don't have to deal with native libs. Have a look at the AppendExample. It does exactly what you want to do. Look out for the latest version. See below for AppendExample to get an idea how it works.

    Movie[] inMovies = new Movie[]{MovieCreator.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-deutsch-audio.mp4"))),
            MovieCreator.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-english-audio.mp4")))};

    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.size() > 0) {
        result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
    }
    if (videoTracks.size() > 0) {
        result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
    }

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

    FileChannel fc = new RandomAccessFile(String.format("output.mp4"), "rw").getChannel();
    fc.position(0);
    out.getBox(fc);
    fc.close();
Sebastian Annies
  • 2,227
  • 1
  • 17
  • 37
  • 1
    i have put the isoviewer-2.0-RC-22.jar library, and after i copied the code it says that AppendExample.class does not exist, do i have to create it, or is it from the library? – rosu alin Mar 29 '13 at 08:46
  • 1
    you don't need it. I just use it to access the classpath – Sebastian Annies Mar 29 '13 at 19:10
  • the mp4parser library does not work on android for more than 2 files! – Alin Apr 03 '13 at 19:20
  • @SebastianAnnies i have tried using your code but i am struck at a point, and really want your help to get this done.... Can you please help me explain how would i write the code using your api to concatenate two video files for example i have 2 files named vid_1.mp4 and vid_2.mp4, and it gets combined to a file name vid_3.mp4 – Kumar Vivek Mitra Jul 11 '13 at 12:39
  • nice one. a bit heavy to have to include isoparser, but for the moment the only non-native solution i have seen – njzk2 Jan 20 '14 at 20:09
  • @SebastianAnnies can u tell me how to access AppendExample from class path and i'm using this lib (isoviewer-2.0-RC-22.jar). – duggu Jun 09 '14 at 06:39
  • @SebastianAnnies please see this question if you have any suggestion, Thanks in advance https://stackoverflow.com/questions/47009530/creating-movie-from-files-by-adding-track-using-com-googlecode-mp4parser-with-wi – Prashant Oct 30 '17 at 10:27
1

In android there is no any inbuilt functionality for combining two audio files, If you are done it through any file operation then this also down to work because its not working as the headers of audio files are different.

I recommended to use a external library FFMPEG for your android application.

Community
  • 1
  • 1
user370305
  • 103,719
  • 23
  • 157
  • 149