编程语言
首页 > 编程语言> > 在另一首Javafx之后播放一首歌

在另一首Javafx之后播放一首歌

作者:互联网

我试图从数据库中获取歌曲位置,然后播放另一首歌曲,但是在这里,它播放数据库中的最后一首歌曲,然后停止播放.我想播放第一首歌,然后播放第二首歌.

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

            }
    }
}

解决方法:

您的代码存在逻辑问题.您不仅尝试更改媒体,还尝试在循环中再次添加所有内容.循环将再次为您构建所有内容,最后,您将获得最后播放的媒体.您需要播放第一个,并在完成时添加第二个,然后播放,依此类推.

用这个美丽代替for循环.

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

标签:java,mysql,javafx-2
来源: https://codeday.me/bug/20191014/1911652.html