使用来自直接音乐流的javaFX从URL播放音频

时间:2016-12-24 19:54:50

标签: java audio javafx audio-streaming

我正在尝试播放来自' https://listen.moe/stream'这是一个动漫音乐的无线电频道,我使用java和javaFX来实现这一点,我正在使用Media,MediaPlayer和MediaView类,但是我的资源文件夹中只有mp3文件才能执行,我不能找到一种方法来检查它是否是可用来源。

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() {}

}

- 媒体信息

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;
}

}

- 主要课程

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);

        }

    });
}

}

正如您所看到的,我尝试使用可用的.mp3文件执行源代码,这是出于测试目的,但是这不会执行任何操作,也不会产生错误。我尝试过使用HTTPSUrlConnection但无法使用它。

当我将鼠标悬停在按钮上时,我似乎也失去了指向光标的能力,我尝试使用-fx-pointer:指针;我尝试过setPointer()方法,但这两种方法都没有用。

我不知道该怎么做,有没有人可以通过告诉我应该在哪里来帮助我?

0 个答案:

没有答案