在另一首Javafx之后播放一首歌 [英] Play one song after the another javafx

查看:101
本文介绍了在另一首Javafx之后播放一首歌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

            }
    }
}

推荐答案

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

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.

用这个美丽代替for循环.

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

这篇关于在另一首Javafx之后播放一首歌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆