2017-01-21 95 views
0
private void initPlayer() throws MalformedURLException{ 

    File dir = new File("../Railway PRS/Videos"); 
    if (!dir.exists() && dir.isDirectory()){ 
     //System.out.println("Cannot find audio source directory: " + dir); 
     Alert alert = new Alert(AlertType.ERROR); 
     alert.setTitle("File Not Found"); 
     alert.setHeaderText("Video File Not Found: "); 
     alert.setContentText("Place the video folder onto the following location: ../Railway/Videos/File.mp4"); 
     alert.showAndWait();    
    } 

    final List<MediaPlayer> players = new ArrayList<>(); 
    File folder = new File("../Railway UTS/Videos"); 
    File[] listofFiles = folder.listFiles(); 
    File file;// = null;   
    for (i = 0; i < listofFiles.length; i++){ 
     file = new File(listofFiles[i].getName()); 
     //Media media = new Media(file.toURI().toURL().toString()); 
     players.add(new MediaPlayer(new Media(file.toURI().toURL().toString()))); 
    } 
    if (players.isEmpty()) { 
     //System.out.println("No audio found in " + dir); 
     Alert alert = new Alert(AlertType.ERROR); 
     alert.setTitle("Audio File Not Found"); 
     alert.setHeaderText("Audio File?"); 
     alert.setContentText("Place the videos onto the following location: ../Railway/Videos/File.mp4"); 
     alert.showAndWait(); 
    } 


    mediaview = new MediaView(players.get(0)); 
    //mediaview1 = new MediaView(players.get(0));   
    for (j = 0; j < players.size(); j++){    
     final MediaPlayer player = players.get(j); 
     final MediaPlayer nextPlayer = players.get((j + 1) % players.size()); 
     player.setOnEndOfMedia(new Runnable() { 
      @Override 
      public void run() { 
       mediaview.setMediaPlayer(nextPlayer); 
       nextPlayer.play(); 
       //nextPlayer.setCycleCount(nextPlayer.INDEFINITE); 
       //player.setCycleCount(MediaPlayer.INDEFINITE);       
      } 
     }); 
    } 
    mediaview.setMediaPlayer(players.get(0));   
    mediaview.getMediaPlayer().play(); 
    basepane.getChildren().addAll(mediaview); 


} 

上述代码仅播放一次视频(播放列表)。我希望它不断播放视频(播放列表)。如果我将循环播放设置为“不确定”,则在播放第二个视频时重复第一个视频意味着我们可以听到第一个视频的声音。除非退出应用程序,否则我希望播放列表不间断播放。无法在JavaFX中连续播放MediaPlayer中的播放列表

回答

0

只需在for循环中放入以下行,它就可以工作。它将循环播放视频/视频。

nameofmediaplayer.seek(DURATION.ZERO);