I have a java project where I have to make a music player that plays either wav or mp3 files. However I can't get my wav or mp3 files to play using the Javafx libraries or with native java libraries. I've checked and made sure the wav and mp3 files I'm using to test aren't corrupted. I'm using Javafx 17.0.2 and JDK 11.
Mini Reproducible Example With 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-info
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;
}
Error Produced
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
So after this didn't work, I tried it with native Java libraries.
Mini Reproducible Example With Native Java Libraries
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();
}
}
Error Produced
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
Extra Information
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