一首接一首地播放 javafx
Play one song after the another javafx
我正在尝试从 DB 获取歌曲位置并一首接一首地播放,但在这里它播放了数据库中的最后一首歌曲并停止播放。我想播放第一首,然后播放第二首。
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);
}
}
我正在尝试从 DB 获取歌曲位置并一首接一首地播放,但在这里它播放了数据库中的最后一首歌曲并停止播放。我想播放第一首,然后播放第二首。
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);
}
}