简体   繁体   English

JavaFx MediaPlayer每小时播放一个mp3

[英]JavaFx MediaPlayer playing one mp3 every hour

I have following code: 我有以下代码:

    Task<Integer> task = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            Calendar tajm = Calendar.getInstance();
            int hour = tajm.get(Calendar.HOUR_OF_DAY);
            int minutes = 0;
            updateMessage(Integer.toString(hour));
            URL resource = getClass().getResource("classico.mp3");
            Media media = new Media(resource.toString());
            MediaPlayer mediaPlayer = new MediaPlayer(media);

            while (true) {
                try {
                    Thread.sleep(60000);
                    tajm = Calendar.getInstance();
                    if (tajm.get(Calendar.HOUR_OF_DAY) != hour) {
                        hour = tajm.get(Calendar.HOUR_OF_DAY);
                        updateMessage(Integer.toString(hour));
                        System.out.println("Hour has changed to: " + hour);

                        mediaPlayer.play();
                    } else {
                        hour = tajm.get(Calendar.HOUR_OF_DAY);
                        System.out.println("Hour has not changed, it's still: " + hour);
                        updateMessage(Integer.toString(hour));
                    }
                } catch (InterruptedException ie) {
                    //System.err.print("...");
                }
            }
        }
    };

The song plays only one time, at first change of hour. 这首歌只播放一次,第一次换小时。 Any idea why? 知道为什么吗?

I have one additional question, not related: is this normal that my javafx programs takes like 20 seconds to start? 我有一个额外的问题,不相关:这是正常的,我的javafx程序开始需要20秒? It's kind of annoying;p Normal applications, with only JavaSE starts almost immedietally. 这有点令人讨厌; p普通应用程序,只有JavaSE几乎可以立即启动。

Suggested Alternate Approach - Create a new player as needed 建议的替代方法 - 根据需要创建新的播放器

I'd advise creating a new MediaPlayer each time you want to play your mp3, rather than reusing an existing MediaPlayer. 我建议每次你想播放你的mp3时创建一个新的MediaPlayer,而不是重用现有的MediaPlayer。 By creating a new MediaPlayer each time, you only need to consume the media playing resources when you need them, rather than allocating them and letting them set idle between play times. 通过每次创建一个新的MediaPlayer,您只需要在需要时使用媒体播放资源,而不是分配它们并让它们在播放时间之间设置为空闲。

For example, call all of the media related calls together rather than at separate points, and let the garbage collector throw away the media resources when they are no longer required. 例如,将所有与媒体相关的调用一起调用而不是在不同的点调用,并让垃圾收集器在不再需要时丢弃媒体资源。

Media media = new Media(resource.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();

How to reuse an existing player (if required) 如何重用现有播放器(如果需要)

If you do want to use your current code rather and reuse the same media player, then you can place a couple of calls before your play call to ensure that on subsequent play calls the player tries to play the mp3 from the start rather than the end (which results in a no-op). 如果您确实想要使用当前代码并重复使用相同的媒体播放器,那么您可以在播放呼叫之前拨打几个电话,以确保在后续播放呼叫中播放器尝试从头开始播放mp3而不是播放(这导致无操作)。

mediaPlayer.setStartTime(Duration.ZERO);
mediaPlayer.seek(Duration.ZERO);
mediaPlayer.play();

Suggestion - Periodically trigger playing your mp3 using a Timeline rather than a Task 建议 - 使用时间轴而不是任务定期触发播放mp3

You don't really need a separate thread and task which adds a bit more complication, you can use a Timeline to trigger the play event each hour. 你真的不需要一个单独的线程和任务,这会增加一点复杂性,你可以使用时间轴来每小时触发一次游戏事件。

Try using mediaPlayer.playFromStart() 尝试使用mediaPlayer.playFromStart()

Also: any unchecked exception thrown from inside your while loop will exit your program. 另外:从while循环内部抛出的任何未经检查的异常都将退出程序。 As a test you can change your current catch(InterruptedException) to catch(Throwable) and print the error to see if that is the case. 作为测试,您可以将当前捕获(InterruptedException)更改为catch(Throwable)并打印错误以查看是否是这种情况。

Another approach would be to use a sheduled executor to execute your task (See Javadoc for SceduleThreadPooExecutor) 另一种方法是使用一个被执行的执行器来执行你的任务(参见Javadoc for SceduleThreadPooExecutor)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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