如何从 mysql 数据库 (JAVA Swing) 在 MediaListPlayer 上添加多个视频?

How to add multiple videos on MediaListPlayer from mysql database (JAVA Swing)?

我陷入了困境。 :( 我需要从我的数据库中获取视频路径。而且我不知道如何将它添加到媒体播放列表中。我使用了 LinkedList 但它似乎不起作用。我需要获取数据库并循环播放它们。

将它们添加到 playlist/mediaList 是我的主要问题。我正在使用 vlcj 1.2.0 jar。请帮帮我。

    public static void video() throws InterruptedException{
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    Canvas canvas = new Canvas();
    canvas.setBackground(Color.black);
    CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
    mediaPlayer.setVideoSurface(videoSurface);
    MediaListPlayer mediaListPlayer = mediaPlayerFactory.newMediaListPlayer();
    mediaListPlayer.addMediaListPlayerEventListener(new MediaListPlayerEventAdapter() {
        @Override
        public void nextItem(MediaListPlayer mediaListPlayer, libvlc_media_t item, String itemMrl) {
            System.out.println("nextItem()");
        }
    });
    mediaListPlayer.setMediaPlayer(mediaPlayer); // <--- Important, associate the media player with the media list player
    vid.add(canvas);
    MediaList mediaList = mediaPlayerFactory.newMediaList();
    String[] options = {};
    LinkedList<String> video = new LinkedList<>(); 
    try{
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost/mediaplaylist";
        Connection conn = DriverManager.getConnection(url,"root","");
        Statement stmt = conn.createStatement();
        String sqlSelect= "select videos from media";
            ResultSet rs = stmt.executeQuery(sqlSelect);
        while(rs.next()){
            String vids= rs.getString("videos");
            video.add(vids);
             int r= video.size();
         for(int s=0; s<r; s++){
             mediaList.addMedia(video.get(s), options);  
        }
       mediaListPlayer.setMediaList(mediaList);
            mediaListPlayer.setMode(MediaListPlayerMode.LOOP);
            mediaListPlayer.play();
         }
    }catch (Exception ex) {
    
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

已解决。这成功了。

   String mrl = new File(vids).getAbsolutePath();

我真傻。 >.< 这是最终代码,我将视频路径保存在 mysql 数据库中:

   public static void video() throws InterruptedException{
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    Canvas canvas = new Canvas();
    canvas.setBackground(Color.black);
    CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
    mediaPlayer.setVideoSurface(videoSurface);
    MediaListPlayer mediaListPlayer = mediaPlayerFactory.newMediaListPlayer();
    mediaListPlayer.addMediaListPlayerEventListener(new MediaListPlayerEventAdapter() {
        @Override
        public void nextItem(MediaListPlayer mediaListPlayer, libvlc_media_t item, String itemMrl) {
            System.out.println("nextItem()");
        }
    });
    mediaListPlayer.setMediaPlayer(mediaPlayer); // <--- Important, associate the media player with the media list player
    vid.add(canvas);
    MediaList mediaList = mediaPlayerFactory.newMediaList();
    String[] options = {};
    LinkedList<String> video = new LinkedList<>(); 
    try{
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost/mediaplaylist";
        Connection conn = DriverManager.getConnection(url,"root","");
        Statement stmt = conn.createStatement();
        String sqlSelect= "select * from media";
            ResultSet rs = stmt.executeQuery(sqlSelect);
        while(rs.next()){
            String vids= rs.getString("videos");

        String mrl = new File(vids).getAbsolutePath();
            System.out.println(mrl);

          mediaList.addMedia(mrl); 
          System.out.println(mediaList.items());
          mediaList.mediaListInstance();
          mediaListPlayer.setMediaList(mediaList);
          mediaListPlayer.setMode(MediaListPlayerMode.LOOP);
          mediaListPlayer.play();
        } 

    }catch (Exception ex) {
    
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
    }
    }