附加视频无法正常使用 - Android

时间:2021-09-19 12:30:05

I am using Sebastian Annies example for the mp4parser where I append 3 videos. The result should be one video that plays all the three videos simultaneously. However, I get one video that plays the last video three times. Here is my code...

我正在使用Sebastian Annies示例为mp4parser添加3个视频。结果应该是一个视频同时播放所有三个视频。但是,我得到一个播放最后一次视频三次的视频。这是我的代码......

            //    int i = number of videos....
      try {         
        String[] f = new String[i];

        for (int count = 0; count < i; count++) {
            f[count] = "/sdcard/vid" + i + ".mp4";
        }

        Movie[] inMovies = new Movie[i];

        for (int count = 0; count < i; count++) {
            inMovies[count] = MovieCreator.build(f[count]);
        }

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

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

        FileChannel fc = new RandomAccessFile(String.format
                ("/sdcard/output.mp4"),
                "rw").getChannel();
        out.writeContainer(fc);
        fc.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    VideoView v = (VideoView) findViewById(R.id.videoView1);
    //      v.setVideoPath("/sdcard/aaa" + i + ".mp4");
    v.setVideoPath("/sdcard/output.mp4");
    v.setMediaController(new MediaController(this));
    v.start();

I dont know why it isn't doing what it's supposed to do. Please help me. Thanks

我不知道为什么它没有做它应该做的事情。请帮帮我。谢谢

1 个解决方案

#1


2  

Your problem is that you're filling the input file-names with the same file:

您的问题是您使用相同的文件填充输入文件名:

for (int count = 0; count < i; count++) {
        f[count] = "/sdcard/vid" + i + ".mp4";
}

Should be

for (int count = 0; count < i; count++) {
        f[count] = "/sdcard/vid" + count + ".mp4";
    }

You might be better off, for readability, to make i the loop variable, and have count be the number of files.

为了便于阅读,您可能会更好,使我成为循环变量,并且计数是文件数。

#1


2  

Your problem is that you're filling the input file-names with the same file:

您的问题是您使用相同的文件填充输入文件名:

for (int count = 0; count < i; count++) {
        f[count] = "/sdcard/vid" + i + ".mp4";
}

Should be

for (int count = 0; count < i; count++) {
        f[count] = "/sdcard/vid" + count + ".mp4";
    }

You might be better off, for readability, to make i the loop variable, and have count be the number of files.

为了便于阅读,您可能会更好,使我成为循环变量,并且计数是文件数。