1

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.

Enkrypton
  • 39
  • 1
  • 2
  • 15
  • Strange. I can download the file and play it, but I cannot play it directly from the URL with any software. *Edit:* Looks like that URL is sending back an HTTP 301 redirect. That’s probably the reason. – VGR Jun 01 '17 at 02:19
  • @VGR Do you have any suggestions on a workaround so I'm able to use a URL source for my audio? – Enkrypton Jun 01 '17 at 02:36

1 Answers1

1

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");
VGR
  • 40,506
  • 4
  • 48
  • 63
  • in fact, i actually originally had it in https: but that gave me the following exception: https://pastebin.com/HhVQKJc8 – Enkrypton Jun 01 '17 at 03:07
  • Are you using JavaFX 8? If not, is there a reason not to use it? – VGR Jun 01 '17 at 03:12
  • i temp added `System.out.println("javafx.runtime.version: " + System.getProperties().get("javafx.runtime.version"));` to my code and it returned `javafx.runtime.version: 8.0.66-b33` . So I suppose i am using JavaFX 8. – Enkrypton Jun 01 '17 at 03:16
  • 1
    seems a fixed bug. https://stackoverflow.com/questions/14725473/javafx-mediaplayer-over-https – TomN Jun 01 '17 at 03:22