1

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

  • 2
    The JavaFX media player is designed for use in a JavaFX application, which you don’t have. Follow the Oracle tutorial on [Incorporating Media Assets Into JavaFX Applications](https://docs.oracle.com/javase/8/javafx/media-tutorial/overview.htm). – jewelsea Feb 13 '22 at 02:15
  • @jewelsea sorry that was a silly mistake I made. I have fixed up the Javafx mre but the same error still occurs. Do you know how I can fix this? The error ```com.sun.media.jfxmedia.MediaException: Could not create player!``` is what I'm stuck on. – harharprogrammer Feb 13 '22 at 03:03
  • Don’t use static scope. Create new MediaPlayers, on the JavaFX thread, e.g. in the start method. Implement media error handling as defined in the template in the [javafx.media package javadoc](https://openjfx.io/javadoc/17/javafx.media/javafx/scene/media/package-summary.html). Ensure you are correctly [requiring the javafx.media module in dependencies and module info](https://stackoverflow.com/questions/64016623/can-not-work-with-mediaplayer-class-javafx-media-is-not-found). – jewelsea Feb 13 '22 at 06:20
  • 1
    @jewelsea Okay, I updated my Javafx code to attempt to fix the MediaPlayer. However, I still get the same error ```com.sun.media.jfxmedia.MediaException: Could not create player!```. I created the MediaPlayer in the start method this time. When I did that, the application started, but immediatly crashed when it tried to construct a MediaPlayer. Any ideas on what could be wrong? – harharprogrammer Feb 13 '22 at 07:03
  • worksforme ... unrelated: don't add modules that you don't need (the example needs controls - which will pull in base and graphics - but does not need swt, swing, fxml, web), and there's no need to open the custom module – kleopatra Feb 13 '22 at 11:31

1 Answers1

1

I could not reproduce your issue.

Your issue is environmental, exactly what it is I could not say.

I advise creating a new project in idea and following the same steps I did and it should work as long as you have the file path correct.

These are the steps I followed to get allow the media to play with your sample app:

  1. Created a new JavaFX project in Idea with OpenJDK 17.0.2 and JavaFX 17.0.2 on Windows 11.

  2. Copy-and-pasted your JavaFX sample code into the new project.

  3. Followed the instructions to add media handling to the project:

  4. Downloaded your mp3 and wav files.

  5. Set the file path to each in turn.

  6. Ran the app and hit the play button for each file.

Both the MP3 and WAV files played without problem.

Your lie in april is nice, I will try to learn it.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 1
    I found out my environmental issue. A few days ago I was freeing up some space on my computer. While doing so, I probably uninstalled an important windows program since my java program ran into errors when I tried to play wav or mp3 files. So I did a windows reset with this link here ```https://www.microsoft.com/en-us/software-download/windows10``` and now I can play mp3 and wav files with javafx and native java libraries. – harharprogrammer Feb 14 '22 at 16:37