简体   繁体   English

如何使MediaPlayer重复播放声音?

[英]How to get MediaPlayer to repeatedly play sound?

My MediaPlayer only plays the sound once. 我的MediaPlayer仅播放一次声音。 When I type audio.play() twice, it still only plays once. 当我键入audio.play()两次时,它仍然只播放一次。 How do I get it to play again when I call audio.play()? 调用audio.play()时如何使其再次播放?

When I tried looking this up, most of the solutions were for Android. 当我尝试查找时,大多数解决方案都适用于Android。 Where they recommended to use AudioFocus, however as my code is just Java and not Android that did not work. 他们建议在哪里使用AudioFocus,因为我的代码只是Java,而不是无法正常工作的Android。

I also tried to use "audio.stop()", in between the two .play()s. 我还尝试在两个.play()之间使用“ audio.stop()”。 But that still did'nt change anything. 但这仍然没有任何改变。

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.File;
    public class audio {
    public static void main(String[] args) {
    com.sun.javafx.application.PlatformImpl.startup(()->{});
    MediaPlayer audio = new MediaPlayer(
            new Media(
                new File("C:\\Users\\User\\Pictures\\Java Projects\\FlappyBird\\sfx_wing.mp3").toURI().toString()));
    audio.play();
    audio.play();
}
}

Currently, the first time I run "audio.play()", the sound correctly plays. 当前,我第一次运行“ audio.play()”时,声音会正确播放。 But when I play it again, it doesn't play. 但是当我再次播放时,它不会播放。

Use audio.seek(audio.getStartTime()); 使用audio.seek(audio.getStartTime()); to play it again. 再次播放。 Check the documentation if you have trouble understanding this line. 如果您在理解此行时遇到问题,请查阅文档。

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

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