0

I searched in many forums and YouTube tutorials for an easy bit of code to just play a sound file (.mp3), but everything I found won't work for me.

I always get the exception that it can't find the file or that something else is wrong, but it always ends in an exception.

Is there something that I have to configure first maybe?

-edit- I tried the following code again to show what exeption i get:

Here is my code

After adding the JFXPanel i got the exeption: Exception in thread "main" MediaException: MEDIA_UNAVAILABLE : D:\bip.mp3 (The System cant find the File) // and yes, i checked if the path is correct.

CRHS
  • 11
  • 3
  • this was the first thing i searched http://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java – Edu G Apr 07 '17 at 10:04
  • 1
    Possible duplicate of [Playing .mp3 and .wav in Java?](http://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java) – Edu G Apr 07 '17 at 10:05
  • Possible duplicate of [JavaFX: "Toolkit" not initialized when trying to play an mp3 file through MediaPlayer class](http://stackoverflow.com/questions/14025718/javafx-toolkit-not-initialized-when-trying-to-play-an-mp3-file-through-mediap) – phihag Apr 07 '17 at 10:30
  • @phihag I found this post already but i tried the solutions and it still doesnt work ... i still get the same result. maybe i did something wrong ? – CRHS Apr 07 '17 at 10:38
  • @CRHS The code you linked to (in an image instead of posting it here - why?) does not include any of the solutions. – phihag Apr 07 '17 at 11:02
  • i wanted to show whats not working and i posted is as a picture link because it wont work with the codeblock stuff from stack overflow ... dont know why. – CRHS Apr 07 '17 at 11:12

2 Answers2

2

To avoid initialization Exception you have to either invoke Application.launch() method or simply instantiate a new JFXPanel() class (even if it isn’t used for anything). This will initiate JavaFxRuntime when application is started

so add below line in your code

 final JFXPanel fxPanel = new JFXPanel();

Import following package

import javafx.embed.swing.JFXPanel;

Now your code will look like this

import java.io.File;
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class Test {

public static void main(String args[]) {

    final JFXPanel fxPanel = new JFXPanel();
    String bip = "D://bip.mp3";  //sound file path
    Media hit = new Media(new File(bip).toURI().toString());
    MediaPlayer mediaPlayer = new MediaPlayer(hit);
    mediaPlayer.play();

  }
}
Sagar Damani
  • 862
  • 7
  • 12
  • Now i got this: Exception in thread "main" MediaException: MEDIA_UNAVAILABLE : D:\Waves.mp3 (The System cant found the File) – CRHS Apr 07 '17 at 11:22
  • Check media file name and path because when I tried by changing bip.mp3 to bip1.mp3(no such file is there) I got the same error, so please check file name with proper cases. – Sagar Damani Apr 07 '17 at 11:27
  • Thats wired ... i checked it 5 times but still the same result ... i even choose the same name for my file to be sure :( – CRHS Apr 07 '17 at 11:31
  • try to add media like this Media hit = new Media("file:///D://bip.mp3"); Three slashes after file.Reference https://superuser.com/questions/352133/what-is-the-reason-that-file-urls-start-with-three-slashes-file-etc – Sagar Damani Apr 07 '17 at 11:43
0

Maybe posting a specific error can solve your problem but anyways I think this can help you. And also I implemented it and works perfectly for me.

This is how I have done it:

List<Media> mediaList = new ArrayList<>();
FileManager files = new FileManager();
files.loadMediaFiles(new File("your music directory goes here.."));

files.getFiles().stream().forEach((media) -> {
    mediaList.add(new Media(media));
});


MediaPlayer mediaPlayer = new MediaPlayer(playList.get(0));
MediaView mediaView = new MediaView(mediaPlayer);

playPauseButton.setOnAction((ActionEvent event) -> {
        if (mediaPlayer.getStatus() == Status.PAUSED || mediaPlayer.getStatus() == Status.READY || mediaPlayer.getStatus() == Status.STOPPED) {
            mediaPlayer.play();
            playPauseButton.setGraphic(pause);
        } else {
            mediaPlayer.pause();
            playPauseButton.setGraphic(play);
        }
    });

You can also checkout my app on github.

lazy
  • 77
  • 10