gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:41:58 26 4
gpt4 key购买 nike

所以我正在用javafx制作一个mp3播放器,它有一个ListView,我可以在其中拖放歌曲,然后选择一首歌曲并按播放键来播放它,你明白了。问题是,使用我当前的代码,我无法播放超过两首歌曲的序列:/(播放选定的歌曲,播放旁边的歌曲,然后停止)。代码如下:

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();
}
}

最佳答案

未测试,但尝试替换

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() 方法的主体。

关于java - 无法播放播放列表中的所有歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29452119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com