Play audio stream with JavaFX


Question

I'm trying to make simple audio player with JavaFX Mediaplayer component. All local files are fine but i want also implement internet radio.

Code:

public static void main(String[] args) throws URISyntaxException {
        new JFXPanel(); //init jfx library
        String u = "http://91.121.164.186:8050";
        Media m=null;
        try {
            m = new Media(u);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        MediaPlayer player = new MediaPlayer(m);
        System.out.println("play");
        player.play();
        player.setVolume(new Double(1));

    }

When I run it like this there is no errors but there is no audio. What's wrong ? What are other posibilities to play radio stream in Java ?

1
3
5/13/2014 1:07:39 PM

In your current example I can see two errors,

  1. You are trying to run a JAVAFX component on a non-Javafx thread, which will result in error. Try running your program inside the start method. Please go through How to use JavaFX MediaPlayer correctly?

  2. The URL you are trying to access must be a Media Compoenent

Try going through this extremely great example on Javafx Media

http://docs.oracle.com/javafx/2/media/EmbeddedMediaPlayer.zip

N.B. The example has lot more data than your require, but its a great example !

1
5/23/2017 11:59:37 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon