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.