简体   繁体   中英

JavaFX Media Player Only Playing One Second of MP3

I am trying to get a mp3 file to play on JavaFX's MediaPlayer from a downloaded file. It is really weird because when I run my code, I hit the play button and it only plays for a second. When I hit the rewind button though, then the mp3 plays. I am not sure if I am doing something wrong.

I have tried using the URL from where I got the mp3 from, but I get an error saying that https protocol is not supported.

Here is my code:

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

    }

}

I am using a Mac with IntelliJ, and I have tried using Eclipse as well without any success.

I'm open to any suggestions on how to get this to work properly or how to get the URL to work.

So I ended up figuring this out on my own after some research.

I found this JDK bug post that sounded just like my issue: https://bugs.openjdk.java.net/browse/JDK-8138754

What I ended up doing was adding this into my code to get my mediaPlayer to work: mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);

I hope that this will help out someone with the same issue someday.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM