Skip to content

Commit

Permalink
Added youtube node (#18)
Browse files Browse the repository at this point in the history
* Added youtube node

* Added youtube node - fixing build

---------

Co-authored-by: Besmir Beqiri <[email protected]>
  • Loading branch information
FlorianKirmaier and besidev authored Dec 4, 2023
1 parent 923e11e commit b8bb5f1
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jpro-youtube/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
implementation "com.sandec.jpro:jpro-webapi:$JPRO_VERSION"
}
20 changes: 20 additions & 0 deletions jpro-youtube/example/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id 'org.openjfx.javafxplugin'
id 'jpro-gradle-plugin'
}

mainClassName = "one.jpro.platform.youtube.example.YoutubeExample"

application {
mainClass = "$mainClassName"
mainModule = moduleName
}

dependencies {
implementation project(':jpro-youtube')
}

javafx {
version = "$JAVAFX_VERSION"
modules = ['javafx.graphics', 'javafx.controls', 'javafx.swing', 'javafx.fxml', 'javafx.media', 'javafx.web']
}
7 changes: 7 additions & 0 deletions jpro-youtube/example/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module one.jpro.platform.youtube.example {
requires one.jpro.platform.youtube;
requires javafx.controls;
requires jpro.webapi;

exports one.jpro.platform.youtube.example;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package one.jpro.platform.youtube.example;

import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import one.jpro.platform.youtube.YoutubeNode;

public class YoutubeExample extends Application {

@Override
public void start(javafx.stage.Stage stage) throws Exception {
var pin = new VBox();

pin.getChildren().add(new StackPane(new YoutubeNode("dQw4w9WgXcQ")));
pin.getChildren().add(new StackPane(new YoutubeNode("oqAsIcoN9MY")));
stage.setScene(new javafx.scene.Scene(new VBox(pin)));

stage.show();
}
}
7 changes: 7 additions & 0 deletions jpro-youtube/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module one.jpro.platform.youtube {

requires jpro.webapi;
requires javafx.graphics;
requires javafx.web;
exports one.jpro.platform.youtube;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package one.jpro.platform.youtube;

import com.jpro.webapi.HTMLView;
import javafx.geometry.Orientation;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import com.jpro.webapi.WebAPI;
import javafx.scene.web.WebView;

/**
* A Node that displays a Youtube Video.
*/
public class YoutubeNode extends StackPane {

protected int PREF_HEIGHT = 340;
protected int PREF_WIDTH = 560;

protected double ratio = 16.0/9.0;

/**
* Creates a new YoutubeNode with the given videoId.
* @param videoId the id of the video to display
*/
public YoutubeNode(String videoId) {

setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
setMinSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
//setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

if(WebAPI.isBrowser()) {
var htmlView = new HTMLView();
htmlView.setContent("<iframe src=\"https://www.youtube.com/embed/"+videoId+"\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>");
getChildren().add(htmlView);
WebAPI.getWebAPI(this, webAPI -> {
var elem = webAPI.getHTMLViewElement(htmlView);
widthProperty().addListener((observable, oldValue, newValue) -> {
webAPI.executeScript(elem.getName()+".firstElementChild.width = "+newValue.intValue()+";");
});
heightProperty().addListener((observable, oldValue, newValue) -> {
webAPI.executeScript(elem.getName()+".firstElementChild.height = "+newValue.intValue()+";");
});
webAPI.executeScript(elem.getName()+".firstElementChild.width = "+getWidth()+";");
webAPI.executeScript(elem.getName()+".firstElementChild.height = "+getHeight()+";");
});
} else {
var webView = new WebView();
webView.getEngine().load("https://www.youtube.com/embed/"+videoId);
getChildren().add(webView);
}
}

@Override
public Orientation getContentBias() {
return Orientation.HORIZONTAL;
}

@Override
protected double computePrefWidth(double height) {
if(height <= -1) {
return PREF_WIDTH;
}
return height * ratio;
}

@Override
protected double computePrefHeight(double width) {
if(width <= -1) {
return PREF_HEIGHT;
}
return width / ratio;
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ include "jpro-sessions"
include "jpro-sessions:example"
include "jpro-html-scrollpane"
include "jpro-html-scrollpane:example"
include "jpro-youtube"
include "jpro-youtube:example"
include "jpro-webrtc"
include "jpro-webrtc:example"
include "tree-showing"
Expand Down

0 comments on commit b8bb5f1

Please sign in to comment.