[英]JavaFX Media Player Only Playing One Second of MP3
我正在尝试从下载的文件中获取要在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);
}
}
我正在将Mac与IntelliJ配合使用,并且尝试使用Eclipse也没有成功。
我愿意就如何使其正常工作或如何使URL工作提出任何建议。
因此,经过一些研究,我最终自行解决了这个问题。
我发现了这个听起来很像我的问题的JDK错误文章: https : //bugs.openjdk.java.net/browse/JDK-8138754
我最终要做的是将其添加到我的代码中,以使我的mediaPlayer正常工作:mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
我希望这将有一天能帮助遇到同样问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.