0

I am writing a game in java, and I am using the Media and MediaPlayer classes in a class called 'Audio' that I am creating to play .mp3 files. Here is my code:

Relevant section of the Main class:

Audio audio = new Audio(audioPath + "oxygen-theme-song.mp3");
audio.play();

Audio class:

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

public class Audio {
    private Media audio;
    private MediaPlayer audioPlayer;
    
    public Audio(String audioPath) {
        audio = new Media(audioPath);
        audioPlayer = new MediaPlayer(audio);
    }
    
    public void play() {
        audioPlayer.play();
    }
    
    public void stop() {
        audioPlayer.pause();
        audioPlayer.setStartTime(Duration.ZERO);
    }
}

This throws the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'Sound-Effects/oxygen-theme-song.mp3'
    at javafx.media/com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:218)
    at javafx.media/javafx.scene.media.Media.<init>(Media.java:393)
    at primecubed.oxygen.Audio.<init>(Audio.java:12)
    at primecubed.oxygen.Main.main(Main.java:118)

The .mp3 file does exist in the correct location, so I can't think of a reason why this is being thrown. Can someone help?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MisterHazza
  • 188
  • 1
  • 10
  • 3
    See the [Javadocs for the media constructor](https://openjfx.io/javadoc/14/javafx.media/javafx/scene/media/Media.html#%3Cinit%3E(java.lang.String)). The value passed must be a valid URI (with a scheme, etc). Don't try to construct the URI yourself; if it's bundled with the application, [create a URI from a resource](https://stackoverflow.com/questions/61531317); if it's on the end user's file system, create a `File` object and convert to a URI. – James_D Aug 11 '20 at 19:04
  • 3
    Does this answer your question? [How to use JavaFX MediaPlayer correctly?](https://stackoverflow.com/questions/22490064/how-to-use-javafx-mediaplayer-correctly) – Tim Hunter Aug 11 '20 at 19:06

0 Answers0