无法播放播放列表中的所有歌曲

Can't play all songs on a playlist

所以我正在用 javafx 制作一个 mp3 播放器,它有一个 ListView 拖放歌曲的地方,然后我 select 一首歌曲并按播放键播放它,你明白了.. 问题是,使用我当前的代码,我无法播放超过两首歌曲的序列:/(播放 selected 歌曲,播放旁边的歌曲然后停止)。 这是代码:

public class MainController implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        list.setItems(tracklist);
    }

    private MediaPlayer player;
    private Duration pausetime = null;
    private int i;

    ObservableList<String> tracklist = FXCollections.observableArrayList();
    ArrayList<File> playlist = new ArrayList<File>();
    ArrayList<MediaPlayer> players = new ArrayList<MediaPlayer>();

    @FXML
    private Slider slider;

    @FXML
    private Button open;

    @FXML
    private Label csong;

    @FXML
    private ListView<String> list;

    public void getsongfromlist(){
        i = list.getSelectionModel().getSelectedIndex();
    }

    public void slide(int a){
        slider.setMin(0.0);
        slider.setValue(0.0);

        player.currentTimeProperty().addListener(new ChangeListener<Duration>(){

            @Override
            public void changed(ObservableValue<? extends Duration> observableValue, Duration duration,
                    Duration current) {
                // TODO Auto-generated method stub
                slider.setMax(players.get(a).getTotalDuration().toSeconds());
                slider.setValue(current.toSeconds());
            }
        });
    }

    public void move(){
        player.seek(Duration.seconds(slider.getValue()));
    }

    public void play(){              //This is where im getting it wrong
        if (player != null){
            player.stop();
        }
        this.player = players.get(i);
        player.play();
        slide(i);
        csong.setText(playlist.get(i).getName());
        for (int j = 0; j < players.size(); j++){
        player.setOnEndOfMedia(new Runnable(){
            @Override public void run(){
            i++;
            list.getSelectionModel().select(i);
            player = players.get(i);
            player.play();
            slide(i);
            csong.setText(playlist.get(i).getName());
            }
        });
    }
    }

    public void handle(DragEvent event) {
        Dragboard db = event.getDragboard();
        if (db.hasFiles()) {
            event.acceptTransferModes(TransferMode.COPY);
        } else {
            event.consume();
        }
    }

    public void dropped(DragEvent event){
        Dragboard db = event.getDragboard();
        event.acceptTransferModes(TransferMode.COPY);
        boolean success = false;
        if (db.hasFiles()){
            success = true;
            for (File file:db.getFiles()){
                tracklist.add(file.getName());
                playlist.add(file);
                try {
                    final Media media = new Media(file.toURI().toURL().toString());
                    final MediaPlayer set = new MediaPlayer(media);
                    players.add(set);
                } catch (MalformedURLException e) {

                    e.printStackTrace();
                }
            }
        }
        event.setDropCompleted(success);
        event.consume();
    }
}

我会尝试改变 if player != null。 尝试不同的值,例如您要播放的歌曲数。

未测试,但尝试更换

for (int j = 0; j < players.size(); j++){
    player.setOnEndOfMedia(new Runnable(){
        @Override public void run(){
        i++;
        list.getSelectionModel().select(i);
        player = players.get(i);
        player.play();
        slide(i);
        csong.setText(playlist.get(i).getName());
        }
    });
}

player.setOnEndOfMedia(new Runnable(){
    @Override
    public void run() {
        i++ ;
        play();
    }
});

并移动线

list.getSelectionModel().select(i);

endOfMedia 侦听器到 play() 方法的主体。