JavaFX - MediaPlayer stop() 方法



在 JavaFX 中,MediaPlayer 類中的 stop() 方法用於停止媒體檔案的播放。當媒體停止後,如果嘗試向前或向後導航,將無法移動到影片中的不同位置。

當呼叫 stop() 方法時,媒體播放器物件會將播放重置為“startTime”,並將“currentCount”重置為零。一旦播放器實際停止,狀態將設定為 MediaPlayer.Status.Stopped。

語法

以下是“MediaPlayer”類的“stop()”方法的語法:

public void stop()

引數

此方法不接受任何引數。

返回值

此方法不返回值。

示例 1

以下是一個演示“MediaPlayer”類的 stop() 方法的基本示例:

在此示例中,我們建立一個顯示影片檔案的應用程式。我們使用 stop() 方法停止影片。停止後,我們檢查媒體播放器的狀態。如果已停止,則在標籤上顯示“媒體已停止”,如果正在播放,則顯示“媒體正在播放”。

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.File;

public class StoMediaExample extends Application {
   @Override
   public void start(Stage primaryStage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      // Create a Media object
      Media media = new Media(mediaPath.toURI().toString());
      // Create a MediaPlayer object and attach the Media object
      MediaPlayer mediaPlayer = new MediaPlayer(media);
      
      // Creating a MediaView object from the MediaPlayer Object
      MediaView viewMedia = new MediaView(mediaPlayer);
      viewMedia.setFitHeight(250);
      viewMedia.setFitWidth(450);
      
      // Create a VBox to hold the label and MediaView
      VBox root = new VBox();
      
      // Use String.valueOf to convert duration to String
      Label stopStatement = new Label("Media is playing right now");
      root.getChildren().addAll(viewMedia, stopStatement);
      
      Scene scene = new Scene(root, 550, 300);

      // Set the Scene to the Stage
      primaryStage.setScene(scene);
      primaryStage.setTitle("Example");
      primaryStage.show();

      mediaPlayer.play();

      // Schedule a stop after 10 seconds
      Duration duration = media.getDuration();
      Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(10), event -> {
         mediaPlayer.stop();
         stopStatement.setText("Media has stopped");
      }));
      timeline.play();
   }

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

輸出

以下是程式碼的輸出:

stopMediaplayer

示例 2

在此示例中,我們正在構建一個 JavaFX 應用程式,該應用程式使用 MediaPlayer 播放媒體檔案。媒體開始播放後,我們等待 20 秒,然後使用 stop() 方法停止播放。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

import java.io.File;

public class StopExample extends Application {
   @Override
   public void start(Stage primaryStage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());

      // Create a MediaPlayer object and attach the Media object
      MediaPlayer mediaPlayer = new MediaPlayer(media);
      MediaView mediaView = new MediaView(mediaPlayer);

      StackPane root = new StackPane();
      root.getChildren().add(mediaView);

      Scene scene = new Scene(root, 550, 270);

      primaryStage.setTitle("Media Player Stop Example");
      primaryStage.setScene(scene);
      primaryStage.show();

      // Start the media player
      mediaPlayer.play();

      // Stop the media player after a delay
      mediaPlayer.setOnReady(() -> {
         new Thread(() -> {
            try {
               // Simulate some processing time
               Thread.sleep(20000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            // Stop the media player
            mediaPlayer.stop();
         }).start();
      });
   }
   public static void main(String[] args) {
      launch(args);
   }
}

輸出

以下是程式碼的輸出,其中媒體將在 20 秒後自動停止。

stopMediaplayer2
廣告

© . All rights reserved.