2016-11-08 69 views
1

我和我的朋友正在編寫一個用於rickroll人的小型javafx 8程序。一旦你打開它,如果你試圖關閉它,它會再次回來,拖走它也不起作用。但是,我們遇到了一個問題:一旦你啓動程序並關閉它,1秒鐘後它重新打開,但mediaplayer凍結,我們無法弄清楚原因。編輯:當你重新打開時,mediaplayer不會立即凍結,1秒後它重新打開凍結。所以它重新打開,打得很好,但如果你等待,並讓它在重新打開後播放1秒鐘,它就會凍結。在javafx中,我的mediaplayer凍結,如果我關閉並重新打開場景

這就是如何MediaPlayer正在創建的:

Media media = new Media(getClass().getResource("/res/video.mp4").toString()); 
    mediaPlayer = new javafx.scene.media.MediaPlayer(media); 
    mediaPlayer.setAutoPlay(true); 
    mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE); 
    MediaView mediaView = new MediaView(mediaPlayer); 

當窗口關閉時,我們暫停視頻1秒鐘,使用此代碼:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() { 
     @Override 
     public void handle(WindowEvent we) { 
     primaryStage.close(); 
     mediaPlayer.pause(); 
      System.out.println("pause"); 
     timeline = new Timeline(new KeyFrame(
      Duration.millis(1000), 
      e -> { 
       openWindow(primaryStage); 
      } 
     )); 
     timeline.setCycleCount(1); 
     timeline.play(); 
     } 
    }); 

終於openWindow方法是什麼打開噸1秒後備份:

private void openWindow(Stage primaryStage){ 
    primaryStage.show(); 
    mediaPlayer.play(); 
} 

我遺漏了很多東西,因爲我覺得這很不正常。如果你認爲你需要更多,我願意把我的完整代碼放在這裏,但是這是101行,看起來有點過頭了。 我要感謝您對您的幫助提前,我很抱歉,如果這個問題不清楚:我沒有真正經歷過...... 親切的問候 Luit意謂

編輯:這裏是我完整的代碼:

package rickroll; 

    import java.util.ArrayList; 
    import javafx.animation.KeyFrame; 
    import javafx.animation.Timeline; 
    import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.media.Media; 
import javafx.scene.media.MediaPlayer; 
import javafx.scene.media.MediaPlayer.Status; 
import static javafx.scene.media.MediaPlayer.Status.PLAYING; 
import javafx.scene.media.MediaView; 
import javafx.stage.Screen; 
import javafx.stage.Stage; 
import javafx.stage.WindowEvent; 
import javafx.util.Duration; 

public class Rickroll extends Application { 
    private ArrayList<Stage> stages = new ArrayList<>(); 
    private Group root = new Group(); 
    private MediaPlayer mediaPlayer; 
    private Timeline focusTimer, timeline; 
    private Scene scene; 

    @Override 
    public void start(Stage primaryStage) { 
     Media media = new Media(getClass().getResource("/res/video.mp4").toString()); 
     mediaPlayer = new javafx.scene.media.MediaPlayer(media); 
//  mediaPlayer.setAutoPlay(false); 
     mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE); 
     MediaView mediaView = new MediaView(mediaPlayer); 
     stages.add(primaryStage); 
     primaryStage.setAlwaysOnTop(true); 
     Button button = new Button(); 
     button.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> Platform.exit()); 

     for(int i = 1; Screen.getScreens().size() >= i; i++){ 

     } 

     root.getChildren().add(mediaView); 
     root.getChildren().add(button); 
     scene = new Scene(
      root, 
      Screen.getPrimary().getBounds().getWidth(), 
      Screen.getPrimary().getBounds().getWidth() 
     ); 
     primaryStage.setScene(scene); 
     Platform.setImplicitExit(false); 

     focusTimer = new Timeline(new KeyFrame(
      Duration.millis(500), 
      e -> { 
       primaryStage.requestFocus(); 
       primaryStage.centerOnScreen(); 
       primaryStage.setMaximized(true); 
      } 
     )); 
     focusTimer.setCycleCount(Timeline.INDEFINITE); 
     focusTimer.play(); 

     primaryStage.setTitle("You just got Rick Rolled!"); 
     openWindow(primaryStage); 

     primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() { 
      @Override 
      public void handle(WindowEvent we) { 
      primaryStage.hide(); 
      mediaPlayer.pause(); 
       System.out.println("pause"); 
      timeline = new Timeline(new KeyFrame(
       Duration.millis(1000), 
       e -> { 
        openWindow(primaryStage); 
       } 
      )); 
      timeline.setCycleCount(1); 
      timeline.play(); 
      focusTimer.pause(); 
      } 
     }); 

    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

    private void openWindow(Stage primaryStage){ 
     primaryStage.show(); 
     mediaPlayer.play(); 
     System.out.println("play"); 
     focusTimer.play(); 
    } 

} 

正如你將會看到的那樣,有一些東西在那裏,這個問題是沒有關係的。就像在多個屏幕上這樣做的開始,以及一個關閉窗口的臨時按鈕。請忽略那部分。

回答

0

如果問題在於它在您打開/關閉之間暫停,那麼因爲您的onCloseRequest處理程序中有mediaPlayer.pause()。但是,如果問題是它不會重新開放再次打開,我無法複製它。另外什麼是for循環的目的?

+0

我試過了,沒用。以下是我的完整代碼: – Luit

+0

由於評論太長,我編輯了我的問題。它現在包含完整的代碼。我贊成你試圖幫助很多!謝謝 – Luit

+0

@Luit更新回答 –

0

檢查下面的代碼(它包含註釋):

import java.net.URISyntaxException; 
import java.util.ArrayList; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javafx.animation.KeyFrame; 
import javafx.animation.PauseTransition; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.media.Media; 
import javafx.scene.media.MediaPlayer; 
import javafx.scene.media.MediaView; 
import javafx.stage.Screen; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class Rickroll extends Application { 

    private ArrayList<Stage> stages = new ArrayList<>(); 

    private Group    root = new Group(); 
    private Scene    scene; 

    private MediaPlayer   mediaPlayer; 
    private Timeline   focusTimer; 

    @Override 
    public void start(Stage primaryStage) { 

     // Initialise the MediaPlayer 
     Media media = null; 
     try { 
      //change here with your video path 
      media = new Media(getClass().getResource("/videos/video.mp4").toURI().toString()); 
      mediaPlayer = new MediaPlayer(media); 
      mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE); 
      // mediaPlayer.setAutoPlay(false) 
     } catch (URISyntaxException ex) { 
      Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); 
     } 

     // Create the Media View 
     MediaView mediaView = new MediaView(mediaPlayer); 

     // Assuming that you want the Media to have the same size as the window 
     //primaryStage.widthProperty() 
     //  .addListener((observable, oldValue, newValue) -> mediaView.setFitWidth(primaryStage.getWidth())) 

     //primaryStage.heightProperty() 
     //  .addListener((observable, oldValue, newValue) -> mediaView.setFitHeight(primaryStage.getHeight())) 

     // ? 
     stages.add(primaryStage); 

     // PrimaryStage 
     primaryStage.setTitle("You just got Rick Rolled!"); 
     primaryStage.setOnCloseRequest(windowEvent -> { 

      // Hide the stage 
      primaryStage.hide(); 

      // Pause the Media Player 
      mediaPlayer.pause(); 
      System.out.println("pause"); 

      // Use a pause transition to wait 1 second - 1000 milliseconds 
      PauseTransition pause = new PauseTransition(); 
      pause.setDuration(Duration.millis(1000)); 
      pause.play(); 
      pause.setOnFinished(finish -> openWindow(primaryStage)); 

      // Pause the focusTimer 
      focusTimer.pause(); 
     }); 

     // Button 
     Button button = new Button(); 
     button.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> Platform.exit()); 

     // ? you want to know the primary screen size? 
     for (int i = 1; Screen.getScreens().size() >= i; i++) { 

     } 

     // Add the children to the root 
     root.getChildren().addAll(mediaView, button); 
     // Scene 
     scene = new Scene(root, Screen.getPrimary().getBounds().getWidth(), Screen.getPrimary().getBounds().getWidth()); 
     primaryStage.setScene(scene); 
     Platform.setImplicitExit(false); 

     // Focus Timer 
     focusTimer = new Timeline(new KeyFrame(Duration.millis(500), e -> { 
      primaryStage.requestFocus(); 
      primaryStage.centerOnScreen(); 
      primaryStage.setMaximized(true); 
     })); 
     focusTimer.setCycleCount(Timeline.INDEFINITE); 
     focusTimer.play(); 

     // Open the primary stage 
     openWindow(primaryStage); 

    } 

    /** 
    * @param args 
    *   the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

    /** 
    * Open the window and start the player 
    * 
    * @param primaryStage 
    */ 
    private void openWindow(Stage primaryStage) { 

     // Show the primary stage 
     primaryStage.show(); 

     // start the media player 
     mediaPlayer.play(); 
     System.out.println("play"); 

     // start the focusTimer 
     focusTimer.play(); 
    } 

} 
+0

此代碼是如此當按下X時,然後程序再次顯示,它將繼續停止。當屏幕不可見時,我不希望歌曲繼續播放。我不完全明白你最後一句話的意思。 – Luit

+0

@Luit編輯已完成.. :) – GOXR3PLUS

+0

但我不希望用戶有能力完全退出平臺。當按下X時,我希望它等待1秒鐘,然後在音樂視頻 – Luit

相關問題