Enkrypton
Enkrypton

Reputation: 49

JavaFX: Use a URL source for audio

I'm able to host my images on imgur for my javafx project like so:

Image circle = new Image("http://imgur.com/7oW7ilC.png");

But when I try to do the same for audio files, it doesn't play the sound (no errors)

Media sound = new Media("http://enkrypton.github.io/filehost2017/hit.mp3");
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();

Is there a way I can use audio files from a URL the same way I use images? Note that this is not a HTTP Error 403 since I'm able to load other images I have on my filehost.

Upvotes: 1

Views: 1801

Answers (1)

VGR
VGR

Reputation: 44308

The problem is that the sound URL is an http: URL, which returns an HTTP 301 response that redirects to an https: URL. This is not considered secure, so Java won’t automatically follow it. For a full discussion of this, see URLConnection Doesn't Follow Redirect.

The easiest solution is to simply change your URL to use https::

Media sound = new Media("https://enkrypton.github.io/filehost2017/hit.mp3");

Upvotes: 1

Related Questions