简体   繁体   中英

JavaFX MediaPlayer - Music stops after 10 seconds

Here's the code, like the title says the music stops after 10ish seconds, i played the file normally in vlc or other programs, it lasts more than 5 minutes.

public void music(){
        String bip = "src/data/fjordmusic.mp3";
        Media hit = new Media(Paths.get(bip).toUri().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(hit);
        mediaPlayer.play();
    }

Your question was already asked and answered here: MediaPlayer stop playing after about 5 seconds

It seems that the garbage collector ereases the MediaPlayer instance after finishing the method. Put the declaration of MediaPlayer above the method and it should work.

MediaPlayer mediaPlayer
public void music(){
    String bip = "src/data/fjordmusic.mp3";
    Media hit = new Media(Paths.get(bip).toUri().toString());
    mediaPlayer = new MediaPlayer(hit);
    mediaPlayer.play();
}

(I'm not able to post comments, so I'm forced to write an answer.)

Try AudioClip instead:

javafx.scene.media.AudioClip;

    public void music(){
        String bip = "src/data/fjordmusic.mp3";
        Media hit = new Media(Paths.get(bip).toUri().toString());
        AudioClip mediaPlayer = new AudioClip(hit.getSource());
        mediaPlayer.play();
    }

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