JavaFX - MediaPlayer getStopTime() 方法



在 JavaFX 中,MediaPlayer 類中的 getStopTime() 方法用於檢索當前載入的媒體的停止時間。

此外,此方法返回 stopTime 屬性的值,該屬性的型別為 duration。如果尚未設定停止時間,它將返回“null”或某個預設值,表示媒體應播放到結尾。

語法

以下是 MediaPlayer 類中 getStopTime() 方法的語法:

public final Duration getStopTime()

引數

此方法不接受任何引數。

返回值

此方法返回一個 duration 例項,表示媒體停止播放的時間。

示例 1

以下是一個演示 MediaPlayer 類中 getStopTime() 方法的基本示例:

在此示例中,我們建立一個 Media 物件和一個 MediaPlayer 物件來播放它。我們使用 setStopTime() 方法將停止時間設定為 60 秒。然後,我們使用 getStopTime() 檢索停止時間,該時間將列印在控制檯上。

import javafx.application.Platform;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
import java.io.File;
public class GetStopTimeEx{
   public static void main(String[] args) {
      Platform.startup(() -> {
         File mediaPath = new File("./audio_video/Hero2.mp3");
         // Create a Media object
         Media media = new Media(mediaPath.toURI().toString());
         MediaPlayer mediaPlayer = new MediaPlayer(media);
         
         // Set the stop time to 1 minutes for the loaded media
         mediaPlayer.setStopTime(Duration.seconds(60));
         
         // Get the stop time using getStopTime() method
         Duration stopTime = mediaPlayer.getStopTime();
         System.out.println("The media will stop playing at: " + stopTime.toSeconds() + " seconds");
      });
   }
}

輸出

以下是程式碼的輸出:

The media will stop playing at: 60.0 seconds

示例 2

在此示例中,我們構建了一個應用程式,該應用程式在 VBox 中載入並播放影片。我們使用 setStopTime() 設定影片的停止時間,然後使用 getStopTime() 獲取此時間。

import javafx.application.Application;
import javafx.application.Platform;
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 GetStopTimeExample 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);

      // Set the stop time in 30 seconds for the media
      mediaPlayer.setStopTime(Duration.seconds(30));

      // creating a MediaView object from the MediaPlayer Object
      MediaView viewmedia = new MediaView(mediaPlayer);
      viewmedia.setFitHeight(280);
      viewmedia.setFitWidth(500);

      // Create a VBox to hold the label and MediaView
      VBox root = new VBox();

      // Get the stop time using getStopTime() method	  
      Duration stopTime = mediaPlayer.getStopTime();
      // Use String.valueOf to convert duration to String
      Label stoptime = new Label("Stop Time: " + String.valueOf(stopTime));
      root.getChildren().addAll(viewmedia, stoptime);

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

      // Set the Scene to the Stage
      primaryStage.setScene(scene);
      primaryStage.setTitle("Example");
      primaryStage.show();
       
      mediaPlayer.play();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

輸出

以下是程式碼的輸出,顯示影片的停止時間(以毫秒為單位)。

getStopTime

示例 3

在此示例中,我們列印 getStopTime() 方法返回的預設值。

import javafx.application.Platform;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
import java.io.File;
public class GetStopTimeEx{
   public static void main(String[] args) {
      Platform.startup(() -> {
         File mediaPath = new File("./audio_video/Hero2.mp3");
		 
         // Create a Media object
         Media media = new Media(mediaPath.toURI().toString());
         MediaPlayer mediaPlayer = new MediaPlayer(media);
         
         // Get the stop time using getStopTime() method
         Duration stopTime = mediaPlayer.getStopTime();
         System.out.println("The media will stop playing at: " + stopTime.toSeconds() + " seconds");
      });
   }
}

輸出

以下是程式碼的輸出,即 getStopTime() 方法返回的預設值。

The media will stop playing at: NaN seconds
廣告

© . All rights reserved.