[英]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。
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$2(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$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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$3(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 庫。
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
鏈接到我用來測試的 Mp3 文件。 https://drive.google.com/file/d/1CvAafbMviQ7nvKyojnem9GK73LJsD6MJ/view?usp=sharing
鏈接到我用來發短信的 Wav 文件。 https://drive.google.com/file/d/1k7a93pZIGY65sGs8BrFMDgeRrgYc0C5k/view?usp=sharing
我正在使用 JDK 11 和 Javafx 17.0.2
系統類型:64 位操作系統,基於 x64 的處理器
處理器:Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz 2.81 GHz
Windows版:Windows 10首頁
我無法重現您的問題。
你的問題是環境問題,具體是什么我不能說。
我建議在 idea 中創建一個新項目並按照我所做的相同步驟進行操作,只要文件路徑正確,它就應該可以工作。
這些是我為允許媒體使用您的示例應用程序播放而遵循的步驟:
在 Windows 11 上使用 OpenJDK 17.0.2 和 JavaFX 17.0.2在 Idea 中創建了一個新的 JavaFX 項目。
將 JavaFX 示例代碼復制並粘貼到新項目中。
按照說明將媒體處理添加到項目中:
下載了您的 mp3 和 wav 文件。
依次為每個設置文件路徑。
運行應用程序並點擊每個文件的播放按鈕。
MP3 和 WAV 文件都可以正常播放。
你四月的謊言很好,我會努力學習的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.