如何在Android中的VideoView中逐个播放多个视频

时间:2017-09-26 08:03:15

标签: android video android-videoview

public class PlayMultipleVide extends Activity {

  VideoView videoView;
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.multiplevideo);
    videoView=(VideoView) findViewById(R.id.videoview);
    MediaController mediaController= new MediaController(this);
    mediaController.setAnchorView(videoView);
    List<String> filedata=GetFiles("/storage/emulated/0/Android/data/com.example.android.camera2video/files");
    //specify the location of media file
    Uri uri=Uri.parse("/storage/emulated/0/Android/data/com.example.android.camera2video/files/"+filedata.get(0));
    //Setting MediaController and URI, then starting the videoView
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(uri);
    videoView.requestFocus();
    videoView.start();
  }

  public static ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<>();
    File f = new File(DirectoryPath);
    f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
      return null;
    else {
      for (File file : files) MyFiles.add(file.getName());
    }
    return MyFiles;
  }
}

这是我的代码。我有一个文件夹文件。在其中,有5个视频,我想在每次启动时在VideoView中逐个播放。我不知道如何一次一个地在videoview中播放视频。请建议我。

1 个答案:

答案 0 :(得分:1)

尝试将listener设置为您的videoView:

void setOnCompletionListener (MediaPlayer.OnCompletionListener l)

MediaPlayer.OnCompletionListener会告诉您视频何时到达EOS(流结束)。然后使用新视频Uri重新开始播放。继续这样做,直到列表中有uris( List filedata )。