-1

I'm trying to play an audio file using MediaPlayer but it's not working and showing

Exception in thread "main" java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'audio.mp3'

Can anyone help me to fix this ?

Here is my code:

package playaudio;

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

public class PlayAudio {

    public static void main(String[] args) {

        String fileName = "audio.mp3";
        Media playFile = new Media(fileName);
        MediaPlayer mediaPlayer = new MediaPlayer(playFile);
        mediaPlayer.play();
    }

}

EDIT: When i try converting the filename to a URI it shows following exception.

Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized

Code:

package playaudio;

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

public class PlayAudio {

    public static void main(String[] args) {

        String fileName = "audio.mp3";
        Media playFile = new Media(Paths.get(fileName).toUri().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(playFile);
        mediaPlayer.play();
    }

}
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
  • 2
    Possible duplicate of [" java.lang.IllegalArgumentException: uri.getScheme() == null! " error using JavaFX to play audio](http://stackoverflow.com/questions/26028044/java-lang-illegalargumentexception-uri-getscheme-null-error-using-jav) – Luis Lavieri Aug 17 '16 at 17:34
  • that doesn't solve my problem. i tried that but shows exception. :/ – MD. Khairul Basar Aug 17 '16 at 18:06

2 Answers2

0

The constructor for Media actually requires a URI, meaning that you will have to convert the filename to a URI, try this:

Media media = new Media(Paths.get(fileName).toUri().toString());

0

You can try this.

InputStream in = new FileInputStream("audio.wav");
AudioStream as = new AudioStream(in);     
AudioPlayer.player.start(as);

But, this will only work for .wav files.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
Meghla Khan
  • 83
  • 2
  • 9