无法使用 Javafx MediaPlayer 播放 wav 或 mp3 文件。也无法使用本机 java 库播放 wav 文件
Can't play wav or mp3 files with Javafx MediaPlayer. Also can't play wav files with the native java library
我有一个 java 项目,我必须制作一个可以播放 wav 或 mp3 文件的音乐播放器。但是,我无法使用 Javafx 库或本机 java 库播放我的 wav 或 mp3 文件。我已经检查并确保我用来测试的 wav 和 mp3 文件没有损坏。我正在使用 Javafx 17.0.2 和 JDK 11.
Javafx
的迷你可复制示例
JavaFxMp3WavPlayer
package mediaplayerjavafx;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class JavaFxMp3WavPlayer extends Application {
public static void main(String[] args) throws MalformedURLException, IOException {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("My");
Button button = new Button("My Button");
Scene scene = new Scene(button, 200, 100);
stage.setScene(scene);
stage.show();
File file = new File("C:\Users\John Doe\MotisHarmony\accounts\yourLieInApril\downloadedMusic\Mp3Test.mp3");
String path = file.toURI().toASCIIString();
Media media = new Media(path);
MediaPlayer mediaPlayer = new MediaPlayer(media);
button.setOnAction(new EventHandler() {
@Override
public void handle(Event arg0) {
runMusicPlayer(mediaPlayer);
}
});
}
public void runMusicPlayer(MediaPlayer mediaPlayer) {
mediaPlayer.play();
}
}
模块信息
module MotisHarmony {
requires javafx.swt;
requires javafx.base;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires javafx.media;
requires javafx.swing;
requires javafx.web;
exports mediaplayerjavafx;
opens mediaplayerjavafx to javafx.graphics;
}
产生错误
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media/javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:519)
at javafx.media/javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:422)
at MotisHarmony/mediaplayerjavafx.JavaFxMp3WavPlayer.start(JavaFxMp3WavPlayer.java:37)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:184)
... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:297)
at javafx.media/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:475)
... 11 more
Exception running application mediaplayerjavafx.JavaFxMp3WavPlayer
所以在这不起作用之后,我尝试使用本机 Java 库。
使用本机 Java 库的迷你可复制示例
JavaWavPlayer
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class JavaWavPlayer {
/**
* @param args the command line arguments
*/
public static Clip clip;
public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
File yourFile = new File("C:\Users\John Doe\MotisHarmony\accounts\yourLieInApril\downloadedMusic\WavTest.wav");
AudioInputStream stream;
AudioFormat format;
DataLine.Info info;
stream = AudioSystem.getAudioInputStream(yourFile);
format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
}
}
产生错误
Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:425)
at javawavplayer.JavaWavPlayer.main(JavaWavPlayer.java:33)
C:\Users\John Doe\Dropbox(Old)\My PC (DESKTOP-P6JNU2B)\Documents\NetBeansProjects\JavaWavPlayer\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\John Doe\Dropbox(Old)\My PC (DESKTOP-P6JNU2B)\Documents\NetBeansProjects\JavaWavPlayer\nbproject\build-impl.xml:936: Java returned: 1
额外信息
Link to the Mp3 file I used to test.
https://drive.google.com/file/d/1CvAafbMviQ7nvKyojnem9GK73LJsD6MJ/view?usp=sharing
Link to the Wav file I used to text.
https://drive.google.com/file/d/1k7a93pZIGY65sGs8BrFMDgeRrgYc0C5k/view?usp=sharing
I am using JDK 11 and Javafx 17.0.2
System Type: 64-bit operating system, x64-based processor
Processor: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz 2.81 GHz
Windows Edition: Windows 10 Home
我无法重现您的问题。
你的问题是环境问题,具体是什么我不能说。
我建议在 idea 中创建一个新项目并按照我所做的相同步骤进行操作,只要文件路径正确,它就应该可以工作。
这些是我为允许媒体使用您的示例应用程序播放而遵循的步骤:
在 Windows 11.
使用 OpenJDK 17.0.2 和 JavaFX 17.0.2 创建了一个 new JavaFX project in Idea
Copy-and-pasted将您的 JavaFX 示例代码放入新项目中。
按照说明将媒体处理添加到项目中:
已下载您的 mp3 和 wav 文件。
依次设置文件路径。
运行 应用程序并点击每个文件的播放按钮。
MP3 和 WAV 文件都可以正常播放。
你四月的谎言很好,我会努力学习的
我有一个 java 项目,我必须制作一个可以播放 wav 或 mp3 文件的音乐播放器。但是,我无法使用 Javafx 库或本机 java 库播放我的 wav 或 mp3 文件。我已经检查并确保我用来测试的 wav 和 mp3 文件没有损坏。我正在使用 Javafx 17.0.2 和 JDK 11.
Javafx
的迷你可复制示例JavaFxMp3WavPlayer
package mediaplayerjavafx;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class JavaFxMp3WavPlayer extends Application {
public static void main(String[] args) throws MalformedURLException, IOException {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("My");
Button button = new Button("My Button");
Scene scene = new Scene(button, 200, 100);
stage.setScene(scene);
stage.show();
File file = new File("C:\Users\John Doe\MotisHarmony\accounts\yourLieInApril\downloadedMusic\Mp3Test.mp3");
String path = file.toURI().toASCIIString();
Media media = new Media(path);
MediaPlayer mediaPlayer = new MediaPlayer(media);
button.setOnAction(new EventHandler() {
@Override
public void handle(Event arg0) {
runMusicPlayer(mediaPlayer);
}
});
}
public void runMusicPlayer(MediaPlayer mediaPlayer) {
mediaPlayer.play();
}
}
模块信息
module MotisHarmony {
requires javafx.swt;
requires javafx.base;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires javafx.media;
requires javafx.swing;
requires javafx.web;
exports mediaplayerjavafx;
opens mediaplayerjavafx to javafx.graphics;
}
产生错误
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media/javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:519)
at javafx.media/javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:422)
at MotisHarmony/mediaplayerjavafx.JavaFxMp3WavPlayer.start(JavaFxMp3WavPlayer.java:37)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:184)
... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:297)
at javafx.media/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:475)
... 11 more
Exception running application mediaplayerjavafx.JavaFxMp3WavPlayer
所以在这不起作用之后,我尝试使用本机 Java 库。
使用本机 Java 库的迷你可复制示例
JavaWavPlayer
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class JavaWavPlayer {
/**
* @param args the command line arguments
*/
public static Clip clip;
public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
File yourFile = new File("C:\Users\John Doe\MotisHarmony\accounts\yourLieInApril\downloadedMusic\WavTest.wav");
AudioInputStream stream;
AudioFormat format;
DataLine.Info info;
stream = AudioSystem.getAudioInputStream(yourFile);
format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
}
}
产生错误
Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:425)
at javawavplayer.JavaWavPlayer.main(JavaWavPlayer.java:33)
C:\Users\John Doe\Dropbox(Old)\My PC (DESKTOP-P6JNU2B)\Documents\NetBeansProjects\JavaWavPlayer\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\John Doe\Dropbox(Old)\My PC (DESKTOP-P6JNU2B)\Documents\NetBeansProjects\JavaWavPlayer\nbproject\build-impl.xml:936: Java returned: 1
额外信息
Link to the Mp3 file I used to test. https://drive.google.com/file/d/1CvAafbMviQ7nvKyojnem9GK73LJsD6MJ/view?usp=sharing
Link to the Wav file I used to text. https://drive.google.com/file/d/1k7a93pZIGY65sGs8BrFMDgeRrgYc0C5k/view?usp=sharing
I am using JDK 11 and Javafx 17.0.2
System Type: 64-bit operating system, x64-based processor
Processor: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz 2.81 GHz
Windows Edition: Windows 10 Home
我无法重现您的问题。
你的问题是环境问题,具体是什么我不能说。
我建议在 idea 中创建一个新项目并按照我所做的相同步骤进行操作,只要文件路径正确,它就应该可以工作。
这些是我为允许媒体使用您的示例应用程序播放而遵循的步骤:
在 Windows 11.
使用 OpenJDK 17.0.2 和 JavaFX 17.0.2 创建了一个 new JavaFX project in IdeaCopy-and-pasted将您的 JavaFX 示例代码放入新项目中。
按照说明将媒体处理添加到项目中:
已下载您的 mp3 和 wav 文件。
依次设置文件路径。
运行 应用程序并点击每个文件的播放按钮。
MP3 和 WAV 文件都可以正常播放。
你四月的谎言很好,我会努力学习的