[A]如何在javafx中重复播放MP3? [英] [A]How to make a MP3 repeat in javafx?

查看:132
本文介绍了[A]如何在javafx中重复播放MP3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的mp3文件在完成后再次重复。但是我无法创建一个循环来重复播放我的文件
(我使用了这段代码,但只有它在完成后播放我文件的第一秒)

i want my mp3 file repeat again after it finished . but i'm unable to create a loop to play my file repeatedly (i used this code but only it plays first second of my file after it finished)

AudioClip myMusic ....   
myMusic.setCycleCount(AudioClip.INDEFINITE);
myMusic.play();

编辑:
i使用MediaPlayer,但它的循环计数器无法正常工作,例如第一次我播放了我的mp3文件它播放了2次,在第3次播放突然停止
第二次我再次运行我的应用程序,它播放1次,在第二次播放它的一半停止
这里是我的代码:

Edited : i used MediaPlayer but it's cycle counter didn't work correctly for example first time i played my mp3 file it played 2 times and in 3rd time of playing suddenly it stops second time i ran again my app and it played 1 time and in half of second time of playing it stops here is my code :

URL resource = getClass().getResource("abcd.mp3");
     MediaPlayer a =new MediaPlayer(new Media(resource.toString()));
     a.setCycleCount(MediaPlayer.INDEFINITE);

 a.play();

任何想法?提前感谢。

推荐答案

我发现我的解决方案使用了setOnEndOfMedia方法:

i found my solution i used setOnEndOfMedia method :

 URL resource = getClass().getResource("abcd.mp3");
 MediaPlayer a =new MediaPlayer(new Media(resource.toString()));
 a.setOnEndOfMedia(new Runnable() {
       public void run() {
         a.seek(Duration.ZERO);
       }
   });
  a.play();

这篇关于[A]如何在javafx中重复播放MP3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆