5

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.

Community
  • 1
  • 1
user1574009
  • 71
  • 2
  • 4
  • 1
    *"not corrupt because it is playing nicely in vlc."* LOL! Media players go to extraordinary lengths to ensure they can play almost any rubbish file thrown at them. If you need to confirm the validity of a file, do it using a program that is designed to check. – Andrew Thompson Aug 27 '12 at 05:20

2 Answers2

5

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    And to get that URL, you can do `new File("skin.mp3").toURI().toString()` – Thilo Aug 27 '12 at 04:16
  • Hey thanks. The IllegalArgumentException has gone by using the `file://e:/skin.mp3` but now i'm struck with another problem `MediaException: MEDIA_INACCESSIBLE : e at javafx.scene.media.Media.(Unknown Source) at Gui.main(gui.java:6)` – user1574009 Aug 27 '12 at 04:16
  • JavaFX docs says "Indicates an error has occurred: although the media may exist, it is not accessible" - not that is particularly useful. From what I've read either you don't have read access to the file or the file is locked by other program...plus lots of other potential issues – MadProgrammer Aug 27 '12 at 04:22
  • the media do have read access because i'm administrator in windows 7 and also played the song in many mp3 players. ( Actually i want to create my own mp3 player ). What should i do now to solve this issue ??? – user1574009 Aug 27 '12 at 04:28
  • Is the MP3 located in drive `e` or drive `E`? – Andrew Thompson Aug 27 '12 at 06:04
  • `E:` , but tried both cases to play the file... I just want to develop my mp3 player in java.. Any links that can help me out...? – user1574009 Aug 27 '12 at 06:28
  • I've not played with JavaFX but you should try a quick google search, I picked up this http://dotneverland.blogspot.com.au/2008/08/simple-mp3-player-in-javafx.html don't know if it's of any worth or not – MadProgrammer Aug 27 '12 at 06:31
  • Okay thanks.. I'm checking it out.. will notify if it works for me or not.. :) – user1574009 Aug 27 '12 at 06:35
4

There are a few problems with the code in this question.

  1. The class needs to be public.
  2. JavaFX 2 applications need to extend the Application class.
  3. JavaFX 2 applications should define a start method.
  4. The locator for the media being created should be a full URI as noted by MadProgrammer.

Even though the question has a javafx-2 tag, I wonder if it is written for JavaFX 1.x JavaFX Script (which is now an unsupported programming language and incompatible with JavaFX 2). If so, I'd recommend coding in Java and using JavaFX 2.x for this rather than JavaFX Script.

On Windows a file representation of an absolute locator of a URI has three slashes after the file protocol. For example, the following is valid:

file:///C:/Users/Public/Music/skin.mp3

For some reason, a single slash will also work (I guess internally Java will interpolate the extra // for the protocol specifier on files or perhaps there is something I don't understand in the URL specification which means that you don't need a // after the protocol).

file:/C:/Users/Public/Music/skin.mp3

One way to check the file uri for something is valid to ask if the file uri exists

System.out.println("File " + filename + " exists? " + new File(filename).exists()); 

After you know your file uri is valid, you can convert it to a string using.

file.toURI().toURL().toExternalForm()

Here is a short sample program for playing some audio in JavaFX using a MediaPlayer with a little bit of error handling, so that it is easier to understand if something goes wrong.

import java.io.File;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.media.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/** plays an audio in JavaFX 2.x */
public class SimpleAudioPlayer extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) throws MalformedURLException {
    final Label status = new Label("Init");
    MediaPlayer mediaPlayer = createMediaPlayer(
      "C:/Users/Public/Music/Sample Music/Future Islands - Before the Bridge.mp3", 
      status
    );
    StackPane layout = new StackPane();
    layout.getChildren().addAll(status);
    stage.setScene(new Scene(layout, 600, 100, Color.CORNSILK));
    stage.show();
    if (mediaPlayer != null) {
      mediaPlayer.play();
    }  
  }

  /** 
   * creates a media player using a file from the given filename path 
   * and tracks the status of playing the file via the status label 
   */
  private MediaPlayer createMediaPlayer(final String filename, final Label status) throws MalformedURLException {
    File file = new File(filename);
    if (!file.exists()) {
      status.setText("File does not exist: " + filename);
    }
    final String mediaLocation = file.toURI().toURL().toExternalForm();
    Media media = new Media(mediaLocation);
    MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.setOnError(new Runnable() {
      @Override public void run() {
        status.setText("Error");
      }
    });
    mediaPlayer.setOnPlaying(new Runnable() {
      @Override public void run() {
        status.setText("Playing: " + mediaLocation);
      }
    });
    mediaPlayer.setOnEndOfMedia(new Runnable() {
      @Override public void run() {
        status.setText("Done");
      }
    });
    return mediaPlayer;
  }
}

Here is a link to an additional example of a JavaFX 2.x media player which plays all of the mp3 files in a given directory sequentially.

jewelsea
  • 150,031
  • 14
  • 366
  • 406