2

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?

2 Answers2

1

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";

pvg
  • 2,673
  • 4
  • 17
  • 31
  • Could you show me how to change the code? Searched around, but nothing worked. –  Dec 13 '15 at 09:32
0

Here is an example on how to use it:

file:///home/username/example.pdf

If you are to lazy to change your file name, just convert it:

(Uri.fromFile(new File("/sdcard/cats.jpg")));

So yeah, it's not really a problem with your code, just your formatting of the path.

user229044
  • 232,980
  • 40
  • 330
  • 338
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83