这是代码,就像标题所说,音乐在10秒后停止,我在vlc或其他程序中正常播放文件,它持续时间超过5分钟。
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();
}
答案 0 :(得分:11)
您的问题已在此处提出并回答: MediaPlayer stop playing after about 5 seconds
看起来垃圾收集器在完成方法后会对MediaPlayer实例进行操作。将MediaPlayer的声明放在方法之上,它应该可以工作。
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();
}
(我无法发表评论,因此我不得不写答案。)
答案 1 :(得分:-1)
尝试使用AudioClip:
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();
}