Play one song after the another javafx

喜欢而已 提交于 2019-12-31 04:41:22

问题


I'm trying to get song location from DB and play one song after the another but in here it plays the last song in the database and stops playing. I want to play the first song, then the second song.

public  class FX_Musicplayer extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception {

        final ArrayList<String> list = new ArrayList<String>();

        try {
            Statement stmt = null;
            // connect to database radio
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/radio", "root", "");
            stmt=conn.createStatement();

                    String sql = "SELECT location FROM Request";
                    ResultSet rs = stmt.executeQuery(sql);

                     while(rs.next()) {
                        list.add(rs.getString(1));
                    }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

            for (int j = 0; j < 3; j++) {

                final Group root = new Group();
                String item = list.get(j);

                System.out.println(item);

                Media media = new Media(list.get(j));
                final MediaPlayer player = new MediaPlayer(media);
                MediaView view = new MediaView(player);

                root.getChildren().add(view);
                Scene scene = new Scene(root, 400, 400, Color.BLACK);
                stage.setScene(scene);
                stage.show();
                player.play();

            player.setOnEndOfMedia(new Runnable() {
                @Override public void run() 
                {       
                    player.stop();
                    return;
                }
                });

            }
    }
}

回答1:


You have a logical issue with your code. Instead of just changing the media, you are trying to add everything again in the loop. The loop just builds everything again for you and at the end you just get the last media playing. You need to play the first one and on its completion, add the second one, play it and so on.

Replace the for loop with this piece of beauty.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Example extends Application {

    final MediaView view = new MediaView();
    Iterator<String> itr ;
    @Override
    public void start(Stage stage) throws Exception {
        final Group root = new Group();
        List<String> list = new ArrayList<String>();
        itr = list.iterator();
        //Plays the first file
        play(itr.next());
        root.getChildren().add(view);
        Scene scene = new Scene(root, 400, 400, Color.BLACK);
        stage.setScene(scene);
        stage.show();
    }
    public void play(String mediaFile){
        Media media = new Media(mediaFile);
        MediaPlayer player = new MediaPlayer(media);
        view.setMediaPlayer(player);
        player.play();
        player.setOnEndOfMedia(new Runnable() {
            @Override
            public void run() {
                player.stop();
                if (itr.hasNext()) {
                    //Plays the subsequent files
                    play(itr.next());
                }
                return;
            }
        });
    }
    public static void main(String[] args) {
        launch(args);
    }
} 


来源:https://stackoverflow.com/questions/28525410/play-one-song-after-the-another-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!