当我启动媒体播放器并点击下一首歌曲按钮时,它会从第一首歌曲开始,按照预期在列表中下载。但是,如果我启动该程序,双击一个mp3文件播放它,然后单击下一首歌曲按钮,列表中的第一首歌曲播放,而不是mp3双击后的mp3。
btn.setOnAction((ActionEvent e) ->
{
if(doubleClicked)
{
player.stop();
media = new Media(rowData.toURI().toString()); // needs to go to next song
player = new MediaPlayer(media);
player.play();
return;
}
if(music.hasNext())
{
try
{
player.stop();
media = new Media(music.next());
player = new MediaPlayer(media);
player.play();
lbl.setText(media.getSource());
}
catch(MediaException a)
{
System.out.println("Unsupported Format");
}
}
});
双击时, rowData
是mp3文件。我尝试过很多东西,但似乎没什么用。我有自己的数据结构,但它与内置的Iterator
和ArrayList
代码几乎相同。
答案 0 :(得分:0)
您不会根据播放列表中的新位置调整Iterator
的位置。根据用户点击的项目索引,您只需使用List.listIterator
将Iterator
替换为新的。{/ p>
以下是使用标签中显示的ListView
和String
的简化示例:
private Iterator<String> songIterator;
@Override
public void start(Stage primaryStage) {
ObservableList<String> songs = FXCollections.observableArrayList("Song 1", "Song 2", "Song 3", "Song 4");
// start with first song
songIterator = songs.iterator();
Label songLabel = new Label(songIterator.next());
ListView<String> lv = new ListView(songs);
MultipleSelectionModel<String> selectionModel = lv.getSelectionModel();
lv.setCellFactory(t -> new ListCell<String>() {
{
setOnMouseClicked(evt -> {
if (evt.getButton() == MouseButton.PRIMARY && evt.getClickCount() == 2) {
evt.consume();
// update label
songLabel.setText(getItem());
// iterator should return next item next
songIterator = songs.listIterator(selectionModel.getSelectedIndex()+1);
}
});
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : item);
}
});
Button btn = new Button();
btn.setText("Next");
btn.setOnAction((ActionEvent event) -> {
if (songIterator.hasNext()) {
songLabel.setText(songIterator.next());
}
});
Scene scene = new Scene(new VBox(songLabel, lv, btn));
primaryStage.setScene(scene);
primaryStage.show();
}