JavaFX MediaPlayer滞后

时间:2018-02-16 14:18:42

标签: javafx media-player

当我通过MediaPlayer听音乐并做一些动作时,音乐会滞后。有些操作例如是编写代码,在谷歌搜索等。试试这个

public class Example extends Thread {
@Override
public void run() {
    MediaPlayer mediaPlayer = new MediaPlayer(new Media("http://cdndl.zaycev.net/play/1435335/8AXraqSa7-NfuEyu2cxQh27JI8BUZIt7wgWuJQJRZatUrSYkwtWXT931qxVSZvBSlhH_M09RfScoMwGZDHZeU_an_KsTccAzSHCVwGREBZ3pUCNm-dNBK1c2kzB5vbfhyH2cA_9CL86EG2VV0li8THuxDdM?dlKind=play&format=json"));
    mediaPlayer.setOnEndOfMedia(() -> {
        synchronized (Example.this) {
            Example.this.notify();
        }
    });

    mediaPlayer.play();

    synchronized (this) {
        try {
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}

实施例

(function() {
'use strict';

var herId = getParameterByName('Q');
window.setTimeout(function() {
    var el = document.querySelector('.form-control');
    el.value = herId;
}, 200);

window.setTimeout(function() {
    var e = jQuery.Event("keydown");
    e.which = 32; // # Some key code value
    $("input").val(String.fromCharCode(e.which));
    $("input").trigger(e);
}, 1600);
})();

function getParameterByName(name, url) {
   if (!url) url = window.location.href;
   name = name.replace(/[\[\]]/g, "\\$&");
   var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
   results = regex.exec(url);
   if (!results) return null;
   if (!results[2]) return '';
   return decodeURIComponent(results[2].replace(/\+/g, " "));
}

重要的是我有很好的网速。我的问题是什么?

1 个答案:

答案 0 :(得分:0)

这是一个代码示例。在窗口中你可以放一个带有"播放音乐的标签" LOLZ

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        Media pick = new Media("put.mp3"); // replace this with your own audio file
        MediaPlayer player = new MediaPlayer(pick);

        // Add a mediaView, to display the media. Its necessary !
        // This mediaView is added to a Pane
        MediaView mediaView = new MediaView(player);

        // Add to scene
        Group root = new Group(mediaView);
        Scene scene = new Scene(root, 500, 200);

        // Show the stage
        primaryStage.setTitle("Media Player");
        primaryStage.setScene(scene);
        primaryStage.show();

        // Play the media once the stage is shown
        player.play();
    }

    public static void main(String[] args) {
         launch(args);
    }

}