Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.testingmedia;
- import javafx.application.Application;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- import javafx.scene.media.Media;
- import javafx.scene.media.MediaPlayer;
- import javafx.scene.media.MediaView;
- import java.io.File;
- import java.io.IOException;
- public class HelloApplication extends Application {
- @Override
- public void start(Stage stage) throws IOException {
- FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
- Scene scene = new Scene(fxmlLoader.load(), 320, 240);
- stage.setTitle("Hello!");
- stage.setScene(scene);
- stage.show();
- String file = "https://www.youtube.com/watch?v=rcp3tZ3XFyQ";
- Media media = new Media(new File(file).toURI().toString());
- MediaPlayer mediaPlayer = new MediaPlayer(media);
- MediaView mediaView = new MediaView(mediaPlayer);
- Button button1 = new Button("Play");
- Button button2 = new Button("Pause");
- Button button3 = new Button("Stop");
- button1.setOnAction(e -> {
- mediaPlayer.play();
- });
- button2.setOnAction(event -> {
- mediaPlayer.stop();
- });
- button3.setOnAction(event -> {
- mediaPlayer.stop();
- });
- GridPane layout = new GridPane();
- layout.setHgap(10);
- layout.setVgap(10);
- //layout.add(mediaView, 0, 0);
- layout.add(mediaView, 1, 0);
- layout.add(button1, 0, 1);
- layout.add(button2, 1, 1);
- layout.add(button3, 2, 1);
- Scene scene1 = new Scene(layout, 300, 200);
- stage.setTitle("Coding a MediaView");
- stage.setScene(scene);
- stage.show();
- }
- public static void main(String[] args) {
- launch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement