Advertisement
Guest User

Untitled

a guest
Mar 8th, 2023
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. package com.example.viewingthemedia;
  2.  
  3. import javafx.application.Application;
  4. import javafx.fxml.FXMLLoader;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.layout.GridPane;
  8. import javafx.scene.media.Media;
  9. import javafx.scene.media.MediaPlayer;
  10. import javafx.scene.media.MediaView;
  11. import javafx.stage.Stage;
  12.  
  13. import java.io.File;
  14. import java.io.IOException;
  15.  
  16. public class HelloApplication extends Application {
  17.     @Override
  18.     public void start(Stage stage) throws IOException {
  19.         FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
  20.         Scene scene = new Scene(fxmlLoader.load(), 320, 240);
  21.         stage.setTitle("Hello!");
  22.         stage.setScene(scene);
  23.         stage.show();
  24.  
  25.         String file = "https://www.youtube.com/watch?v=rcp3tZ3XFyQ";
  26.         Media media = new Media(new File(file).toURI().toString());
  27.         MediaPlayer mediaPlayer = new MediaPlayer(media);
  28.         MediaView mediaView = new MediaView(mediaPlayer);
  29.  
  30.         Button button1 = new Button("Play");
  31.         Button button2 = new Button("Pause");
  32.         Button button3 = new Button("Stop");
  33.  
  34.         button1.setOnAction(e -> {
  35.             mediaPlayer.play();
  36.         });
  37.  
  38.         button2.setOnAction(event -> {
  39.             mediaPlayer.stop();
  40.         });
  41.  
  42.         button3.setOnAction(event -> {
  43.             mediaPlayer.stop();
  44.         });
  45.  
  46.         GridPane layout = new GridPane();
  47.         layout.setHgap(10);
  48.         layout.setVgap(10);
  49.  
  50.         //layout.add(mediaView, 0, 0);
  51.         layout.add(mediaView, 1, 0);
  52.         layout.add(button1, 0, 1);
  53.         layout.add(button2, 1, 1);
  54.         layout.add(button3, 2, 1);
  55.  
  56.         Scene scene1 = new Scene(layout, 300, 200);
  57.  
  58.         stage.setTitle("Coding a MediaView");
  59.         stage.setScene(scene);
  60.         stage.show();
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement