Javafx Media class 无法正常工作
Javafx Media class not working without any error
为什么这个程序不播放音乐?我正在尝试使用 JavaFX 制作一个应用程序,以允许用户播放他想播放的任何 MP3 文件。但它根本不起作用!为什么?
绝对没有报错!
package application;
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Start extends Application {
public void start(Stage primStage) {
try {
// Validation for first time start
Alert a = new Alert(AlertType.INFORMATION);
a.setTitle("Welcome");
a.setHeaderText("Welcome to Orpheus!");
a.setContentText("Welcome to Orpheus. Pick the folder in which all your music is located");
Stage stg = (Stage) a.getDialogPane().getScene().getWindow();
stg.getIcons().add(new Image(this.getClass().getResource("harp.png").toString()));
primStage.getIcons().add(new Image(this.getClass().getResource("harp.png").toString()));
a.showAndWait();
DirectoryChooser dc = new DirectoryChooser();
dc.setInitialDirectory(new File(System.getProperty("user.home")));
dc.setTitle("Pick your folder!");
File f = dc.showDialog(primStage);
File[] fileArray = f.listFiles();
boolean[] song = new boolean[fileArray.length];
int j = 0;
if (fileArray != null) {
for (File child : fileArray) {
String ext = "";
int i = child.getName().lastIndexOf('.');
if (i >= 0) {
ext = child.getName().substring(i+1);
}
if (ext.equals("mp3")) {
song[j] = true;
j++;
} else {
song[j] = false;
j++;
}
}
}
j = 0;
ArrayList<File> fileList = new ArrayList<>();
for (boolean b : song) {
if(b) {
fileList.add(fileArray[j]);
}
j++;
}
//New codes!
ArrayList<String> names = new ArrayList<>();
for (File fi : fileList) {
String tmp = fi.getCanonicalPath();
tmp = tmp.replaceAll("\\", "/");
names.add(tmp);
}
Media m = new Media("file:///" + names.get(0));
MediaPlayer mp = new MediaPlayer(m);
mp.play();
//App ends with no sleep method for threads
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
但现在我得到错误:
MediaException: MEDIA_UNAVAILABLE : \file:\C:\Users\Kaboom\Music\Closer.mp3 (The filename, directory name, or volume label syntax is incorrect)
at javafx.scene.media.Media.(Media.java:407)
at application.Start.start(Start.java:75)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
我做了一些研究并替换了一些代码。现在唯一发生的事情是没有音乐播放和应用程序结束 运行。
我做错了什么?
我想通了这个问题出了什么问题。太蠢了
我只需将 MediaPlayer 添加到 MediaView 和 GUI
像这样打开媒体:
File file = new File(names.get(0));
String path = file.toURI().toASCIIString();
Media m = new Media(path);
这将转换路径以使其与 url sintax
兼容
为什么这个程序不播放音乐?我正在尝试使用 JavaFX 制作一个应用程序,以允许用户播放他想播放的任何 MP3 文件。但它根本不起作用!为什么?
绝对没有报错!
package application;
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Start extends Application {
public void start(Stage primStage) {
try {
// Validation for first time start
Alert a = new Alert(AlertType.INFORMATION);
a.setTitle("Welcome");
a.setHeaderText("Welcome to Orpheus!");
a.setContentText("Welcome to Orpheus. Pick the folder in which all your music is located");
Stage stg = (Stage) a.getDialogPane().getScene().getWindow();
stg.getIcons().add(new Image(this.getClass().getResource("harp.png").toString()));
primStage.getIcons().add(new Image(this.getClass().getResource("harp.png").toString()));
a.showAndWait();
DirectoryChooser dc = new DirectoryChooser();
dc.setInitialDirectory(new File(System.getProperty("user.home")));
dc.setTitle("Pick your folder!");
File f = dc.showDialog(primStage);
File[] fileArray = f.listFiles();
boolean[] song = new boolean[fileArray.length];
int j = 0;
if (fileArray != null) {
for (File child : fileArray) {
String ext = "";
int i = child.getName().lastIndexOf('.');
if (i >= 0) {
ext = child.getName().substring(i+1);
}
if (ext.equals("mp3")) {
song[j] = true;
j++;
} else {
song[j] = false;
j++;
}
}
}
j = 0;
ArrayList<File> fileList = new ArrayList<>();
for (boolean b : song) {
if(b) {
fileList.add(fileArray[j]);
}
j++;
}
//New codes!
ArrayList<String> names = new ArrayList<>();
for (File fi : fileList) {
String tmp = fi.getCanonicalPath();
tmp = tmp.replaceAll("\\", "/");
names.add(tmp);
}
Media m = new Media("file:///" + names.get(0));
MediaPlayer mp = new MediaPlayer(m);
mp.play();
//App ends with no sleep method for threads
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
但现在我得到错误:
MediaException: MEDIA_UNAVAILABLE : \file:\C:\Users\Kaboom\Music\Closer.mp3 (The filename, directory name, or volume label syntax is incorrect) at javafx.scene.media.Media.(Media.java:407) at application.Start.start(Start.java:75) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191) at java.lang.Thread.run(Unknown Source)
我做了一些研究并替换了一些代码。现在唯一发生的事情是没有音乐播放和应用程序结束 运行。
我做错了什么?
我想通了这个问题出了什么问题。太蠢了
我只需将 MediaPlayer 添加到 MediaView 和 GUI
像这样打开媒体:
File file = new File(names.get(0));
String path = file.toURI().toASCIIString();
Media m = new Media(path);
这将转换路径以使其与 url sintax
兼容