Playing audio from an URL using javaFX from a direct stream of music

1k Views Asked by At

I am trying to play audio from 'https://listen.moe/stream' which is a radio channel for anime music, i am using java and javaFX to achieve this, i am using the Media, MediaPlayer and MediaView classes for this, however only the mp3 files that i have in my resource folder will execute, and i cannot find a way to check if it is an available source or not.

package realdesigner.louwens.containers;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.text.Text;

import realdesigner.louwens.media.MediaInformation;
import realdesigner.louwens.media.MediaInformation.MediaStatus;

public class ListenContainer extends Scene implements Runnable {

private boolean initialized;

Button status;
Text  header;
MediaPlayer moePlayer;
Media playing;
MediaView view;
MediaInformation mediaInfo;
MediaStatus mediaStatus;


public ListenContainer(BorderPane parent) {
    super(parent);
    init(parent);

    mediaInfo = new MediaInformation();

    getRoot().setStyle("-fx-background-color: #383d52");
}

public void init(BorderPane parent) {
    if(!initialized) {

        setUserAgentStylesheet("style/style.css");

        status = new Button("▶");
        status.setPrefHeight(80);
        status.setPrefWidth(100);
        status.setId("status");

        header = new Text("Currently playing: \r\n {music}");
        header.setId("header");

        playing = new Media("https://archive.org/download/testmp3testfile/testmp3testfile_64kb.m3u");
        if(playing.getMetadata() == null) playing = new Media(ListenContainer.class.getResource("/connectionless/rapgod.mp3").toString());


        moePlayer = new MediaPlayer(playing);
        moePlayer.play();
        moePlayer.setVolume(1);
        view = new MediaView(moePlayer);


        status.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                if(!mediaInfo.isPlaying()) {
                    moePlayer.play();
                    mediaInfo.setPlaying(true);

                } else {
                    moePlayer.pause();
                    mediaInfo.setPlaying(false);
                }

            }

        });

        parent.setLeft(header);
        parent.setRight(status);
    }


}

@Override
public void run() {}

}

-- media Information

package realdesigner.louwens.media;

public class MediaInformation {

private boolean playing = false;

public enum MediaStatus {
    NOT_STARTED(0),
        IN_PROGRESS(1),
            FINISHED(2);

    private int status;
    MediaStatus(int status) {
        this.status = status;
    }

    public int getStatus() {
        return status;
    }   
}

public boolean isPlaying() {
    return playing;
}

public void setPlaying(boolean playing) {
    this.playing = playing;
}

}

-- main class

package realdesigner.louwens;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import realdesigner.louwens.containers.ListenContainer;

public class Main extends Application {

private double xOffset, yOffset;

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

@Override
public void start(Stage stage) throws Exception {
    stage.setTitle("Listen.MOE - Java Edition");
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setWidth(480);
    stage.setHeight(80);
    stage.getIcons().add(new Image(Main.class.getResourceAsStream("/icons/listen.png")));

    BorderPane pane = new BorderPane();

    Scene scene = new ListenContainer(pane);

    moveOnResize(stage, scene);

    stage.setScene(scene);
    stage.show();
}

public void moveOnResize(Stage stage, Scene scene) {
    scene.setOnMousePressed(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            xOffset = stage.getX() - event.getScreenX();
            yOffset = stage.getY() - event.getScreenY();

        }});

    scene.setOnMouseDragged(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            stage.setX(event.getScreenX() + xOffset);
            stage.setY(event.getScreenY() + yOffset);

        }

    });
}

}

as you can see, i try to execute a source with an available .mp3 file, this is for testing purposes, this however does not execute anything and neither does it give an error. I have tried using a HTTPSUrlConnection but failed to be able to use it.

I also seem to have lost the ability to have a pointing cursor when i hover over the button, i have tried using the -fx-pointer: pointer; and i have tried the setPointer() method, but neither of these were of use.

I am out of ideas of what to do, is there anybody that can help me by telling me where i should look?

0

There are 0 best solutions below