JavaFX: Media player

This entry is part 53 of 55 in the series JavaFX

As of now, I believe that the native player provided by Java supports only a few types of encoding such as MP3 and flv files.

Here I work out an example where it will simply play an MP3 file that has a URL link.

package mediaplay;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;


public class MediaPlay extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        
        String path = "http://www.noiseaddicts.com/samples/13.mp3";
        
        Media media = new Media(path);       
        StackPane root = new StackPane();

        MediaPlayer mp = new MediaPlayer(media);       
        mp.setAutoPlay(true);
        MediaView mv = new MediaView(mp);
        
        Label lb = new Label("Playing");
        lb.setFont(Font.font("Times New Roman", 52));
        lb.setTextFill(Color.BLUE);
        
        BorderPane pane = new BorderPane();
        pane.setCenter(lb);

        root.getChildren().addAll(mv, pane);
        Scene scene = new Scene(root, 400, 400);
        stage.setScene(scene);
        stage.show();

        mp.play();
    }
}

media

Series Navigation<< JavaFX: Adding circle to line group
JavaFX: Line and bar chart >>