从Android Studio中播放数组中的顺序视频

问题描述 投票:0回答:2

[我正在尝试在Android的阵列中播放一系列视频,但是当以下代码仅运行阵列中的最后一个元素/视频时播放。

如何循环遍历数组并依次播放视频?

我感觉到循环的延续是在videoView.start()命令之后立即发生的,因此仅播放最后一个。

这里是我的代码的近似值...

    VideoView videoView = (VideoView) findViewById(R.id.videoView);
    String file_location = "path/to/my/files/"; // external storage
    String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements

    for(String filepath: filepaths){
        String path = file_location + filepath;
        videoView.setVideoPath(path);
        videoView.start();
    }

我已经尝试添加setOnCompletionListener并将continue放在onCompletion内,但错误是“在循环外继续”

videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            continue;
        }
    });

我如何依次播放每个视频,并且之间的间隔很小/没有间隔?

android android-mediaplayer
2个回答
1
投票

不要使用循环,因为您应该仅在上一个视频结束后才设置下一个视频的路径,因此,请创建一个名为currentPlayingIndex的字段,而不是循环,使其在每个视频结束后递增,然后从该位置开始设置路径。 ..

喜欢这个

    private int currentPlayingIndex; // Keep this as gloabal variable

    VideoView videoView = (VideoView) findViewById(R.id.videoView);
    String file_location = "path/to/my/files/"; // external storage
    String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements


    String path = file_location + filepaths[currentPlayingIndex];
    videoView.setVideoPath(path);
    videoView.start();

    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            currentPlayingIndex++; //Increment index here
            if(currentPlayingIndex < filepaths.length)
            {
                String newPath = file_location + filepaths[currentPlayingIndex];
                videoView.setVideoPath(newPath);
                videoView.start();
            }else {
                //Add logic here when all videos are played
            }
        }
    });

注意:我不确定在视频之间切换需要多长时间...


0
投票

第一个:您需要在索引0的数组中播放* .mp4。

然后,当视频播放完成时,使用播放器播放下一个索引数组中的* .mp4。

我的英语表达可能不太好,希望您能理解

© www.soinside.com 2019 - 2024. All rights reserved.