.Mp3 file in java using notepad

I know this is a recurring question. check the original here or here .

So my code is to only copy a copy:

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 that I get 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 compile and run it correctly, i.e. include the file jfxrt.jarin the classpath

Note. I use notepad instead of any IDE.

So can someone tell me the reason IllegalArgumentException

Thankx

UPDATE : using file://e:/skin.mp3it 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 highlight this exception.

By the way, I checked the song without ruining it, because it plays great in vlc.

+5
source share
2 answers

API JavaFX

  • URI RFC-2396, java.net.URI.
  • HTTP, FILE JAR URI.

, , , URI.

, - file://path/to/file/skin.mp3.

+5

.

  • .
  • JavaFX 2 Application.
  • JavaFX 2 .
  • URI, MadProgrammer.

javafx-2, , JavaFX 1.x JavaFX Script ( JavaFX 2). , Java JavaFX 2.x , JavaFX Script.

Windows URI . , :

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

- ( , Java // , , -, URL, , t // ).

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

uri - , , uri

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

, , uri , , .

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

JavaFX MediaPlayer , , - .

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

JavaFX 2.x, mp3 .

+4

All Articles