Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

playing .mp3 file in java using notepad

I know this is a repeat question. check original one here or here.

So my code is just the copy paste :

import javafx.scene.media.*;

class Gui {
  public static void main(String[] args) {
    try{
        Media hit = new Media("skin.mp3");
        MediaPlayer mediaPlayer = new MediaPlayer(hit);
        mediaPlayer.play();
    }catch(Exception e){
        e.printStackTrace();
    }
  }
}

The exception which i'm getting is :

java.lang.IllegalArgumentException: uri.getScheme() == null!
        at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:217)
        at javafx.scene.media.Media.<init>(Media.java:364)
        at Gui.main(gui.java:6)

I'm compiling & running it correctly i.e. by including the jfxrt.jar file in classpath

Note: I'm just using notepad instead of any IDE.

So can anyone tell me the reason of IllegalArgumentException

Thankx

UPDATE : By using file://e:/skin.mp3 it worked fine but left me with another exception :

MediaException: MEDIA_INACCESSIBLE : e
        at javafx.scene.media.Media.<init>(Unknown Source)
        at Gui.main(gui.java:6)

So if you can put some light on this exception.

By the way i've checked the song, its not corrupt because it is playing nicely in vlc.

like image 396
user1574009 Avatar asked Aug 27 '12 04:08

user1574009


People also ask

How to play mp3 file in Java?

Player; import java. io. FileInputStream; public class MusicPlay { public static void main(String[] args) { try{ FileInputStream fs = new FileInputStream("audio_file_path. mp3"); Player player = new Player(fs); player.

Does Java support mp3?

It turned out that Java 7 has support for mp3 files: String bip = "example. mp3"; Media hit = new Media(bip); MediaPlayer mediaPlayer = new MediaPlayer(hit); mediaPlayer.


1 Answers

From the JavaFX API docs

  • The supplied URI must conform to RFC-2396 as required by java.net.URI.
  • Only HTTP, FILE, and JAR URIs are supported.

So, I suspect from reading the docs, you need to supply a URI path.

Something like file://path/to/file/skin.mp3 will probably work.

like image 50
MadProgrammer Avatar answered Sep 19 '22 09:09

MadProgrammer