在javafx中的Media Player中重复,重复全部,不重复

时间:2017-03-02 07:55:27

标签: javafx


这是我的代码。

boolean repeat=false, repeatall=false;
static List<MediaPlayer> players = new ArrayList<>();
if(!players.isEmpty()){
        for (int i = 0; i < players.size(); i++) {
            MediaPlayer currentPlayer=players.get(i);
            //MediaPlayer nextPlayer=players.get((i+1) % players.size());
            int j=i;
            currentPlayer.setOnEndOfMedia(new Runnable() {
                @Override
                public void run() {
                    currentPlayer.stop();
                    if(repeat){
                        currentPlayer.stop();
                        mediaView.setMediaPlayer(currentPlayer);
                        currentPlayer.play();
                    }else if(repeatall){
                        MediaPlayer nextPlayer=players.get((j+1) % players.size());
                        mediaView.setMediaPlayer(nextPlayer);
                        nextPlayer.play();
                    }else if(!repeatall){
                        if(j<players.size()){
                            MediaPlayer nextPlayer=players.get((j+1));  //Error Here. When Players is playing last index
                            mediaView.setMediaPlayer(nextPlayer);
                            nextPlayer.play();
                        }
                    }
                }
            });
        }
    }


这是我的媒体播放器代码,但它显示一些错误。  
错误显示

MediaPlayer nextPlayer=players.get((j+1)); 

就行了
当玩家是最后一个索引

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at am.fx.main.node.audioPlayer.PlayMenus$12.run(PlayMenus.java:243) \\That Line
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1294163646.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)


能否请你帮我纠正这段代码。

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。

if(j<players.size()-1){
     MediaPlayer nextPlayer=players.get((j+1));
     mediaView.setMediaPlayer(nextPlayer);
     nextPlayer.play();
}

解决了问题。