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.