Search code examples
javacompilationmp3

Java: Error trying to play a mp3 file


I want to play a simple mp3 file. I have this code:

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class MelodyPlay {

    public static void main(String[] args) {
        String bip = "/Users/username/Downloads/melodytest.mp3";
        Media hit = new Media(bip);
        MediaPlayer mediaPlayer = new MediaPlayer(hit);
        mediaPlayer.play();
    }
}

However, I get this error:

java.lang.IllegalArgumentException: uri.getScheme() == null! uri ==...

What am I missing?


Solution

  • The parameter to Media's constructor has to be a valid URI. If you want it to be a file, make it a file:// URI, like this:

    String bip = "file:///Users/username/Downloads/melodytest.mp3";