【问题标题】:JavaFX Media Player Only Playing One Second of MP3JavaFX 媒体播放器只播放一秒钟的 MP3
【发布时间】:2019-11-27 22:33:26
【问题描述】:

我正在尝试从下载的文件中获取要在 JavaFX 的 MediaPlayer 上播放的 mp3 文件。这真的很奇怪,因为当我运行我的代码时,我点击了播放按钮,它只播放了一秒钟。但是,当我按下倒带按钮时,会播放 mp3。我不确定我是否做错了什么。

我已尝试使用我从中获取 mp3 的 URL,但我收到一条错误消息,指出不支持 https 协议。

这是我的代码:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.net.URL;
import java.io.InputStream;
import java.nio.file.StandardCopyOption;

import static java.nio.file.Files.createTempFile;

public class JavaFXApplet extends Application{

    //private static final String MEDIA_URL = "https://www.bensound.com/bensound-music/bensound-summer.mp3";

    @Override
    public void start(Stage primaryStage) {
        Media media = new Media("file:///Users/mycomputer/Downloads/bensound-summer.mp3");
        //Media media = new Media(MEDIA_URL);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        MediaView mediaView = new MediaView(mediaPlayer);
        Button playButton = new Button(">");
        playButton.setOnAction(e -> {mediaPlayer.play();});

        Button pauseButton = new Button("||");
        pauseButton.setOnAction(e-> mediaPlayer.pause());

        Button rewindButton = new Button("<<");
        rewindButton.setOnAction(e -> mediaPlayer.seek(Duration.ZERO));

        Slider slVolume = new Slider();
        slVolume.setPrefWidth(150);
        slVolume.setMaxWidth(Region.USE_PREF_SIZE);
        slVolume.setMinWidth(30);
        slVolume.setValue(50);
        mediaPlayer.volumeProperty().divide(100);

        HBox hBox = new HBox(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(playButton, pauseButton, rewindButton, new Label("Volume"), slVolume);

        BorderPane pane = new BorderPane();
        pane.setCenter(mediaView);
        pane.setBottom(hBox);

        Scene scene = new Scene(pane, 650, 500);
        primaryStage.setTitle("Test Player");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setOnCloseRequest(windowEvent -> {
            mediaPlayer.stop();
        });

    }

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

    }

}

我正在使用带有 IntelliJ 的 Mac,我也尝试使用 Eclipse,但没有任何成功。

我愿意接受有关如何使其正常工作或如何使 URL 正常工作的任何建议。

【问题讨论】:

  • 1.我无法重现“它只播放一秒钟”,它在 Win env 中正常工作。 (我只更改了路径“”file:///e:/bensound-summer.mp3“”)。 2.你想玩“在线”还是下载文件?
  • 我也无法在使用 JavaFX 13.0.1 的 Windows 10 上重现该问题。 httpsfile URL 都有效。
  • @Oleksandr 好吧,我正试图让它发挥任何一种方式。如果我可以让它与https一起使用,那就更好了。会不会是我的 IntelliJ 或 Eclipse 版本?
  • @Perdue 好吧,IDE 版本(IntelliJ 或 Eclipse)没有任何意义。我唯一的想法是 rn -> 请尝试将 mediaPlayer 声明为类成员。
  • @Oleksandr 将 mediaPlayer 声明为类成员是什么意思?你的意思是把它从Start课程中拿出来吗?我试过了,但似乎没有帮助。

标签: java javafx


【解决方案1】:

所以经过一番研究,我最终自己解决了这个问题。

我发现这个 JDK 错误帖子听起来就像我的问题:https://bugs.openjdk.java.net/browse/JDK-8138754

我最终将其添加到我的代码中以使我的 mediaPlayer 工作: mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);

我希望有一天这能帮助遇到同样问题的人。

【讨论】:

  • 不错的调查
猜你喜欢
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多