explore the media in style with jxbrowser in javafx

Hey there, I'm struggling a bit with English so bear with me. I've been working on a project using JavaFX and JxBrowser, but as a novice, I encountered an issue with executing my JavaScript script. It's not running properly. I tried following the instructions provided on this site that used jQuery (here), but unfortunately, it didn't work for me. I suspect that the problem might be related to media access because my JavaScript code requires access to the computer’s media via the browser object. The code works fine on Chrome but encounters issues on JxBrowser.

I would greatly appreciate it if you could take a look at my code and possibly help me correct it for proper functionality. Thank you! Below are snippets of my Java FX code including the Main file, HTML code, and JavaScript script (I omitted the .css file as it's lengthy and poses no issues, since the .html file functions perfectly on Chrome).

Main.java

public class Main extends Application {

@Override
public void start(Stage primaryStage) {
    Browser browser = new Browser();
    BrowserView view = new BrowserView(browser);
    StackPane pane = new StackPane();
    pane.getChildren().add(view);
    Scene scene = new Scene(pane,500,400);
    primaryStage.setScene(scene);
    primaryStage.show();
    browser.addLoadListener(new LoadAdapter() {
@Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
    if (event.isMainFrame()) {
       event.getBrowser().executeJavaScript("$('button').hide();")
    }
}); 

InputStream urlStream = getClass().getResourceAsStream("/html/index.html");
    String html = null;
 try (BufferedReader urlReader = new BufferedReader(new InputStreamReader (urlStream))) {
     StringBuilder builder = new StringBuilder();
     String row;
     while ((row = urlReader.readLine()) != null) {
         builder.append(row);
     }
     html = builder.toString(); 
 }  catch (IOException e) {
     throw new RuntimeException(e);
 }

 browser.loadHTML(html);  

public static void main(String[] args) {
    launch(args);
} 

}

index.html

<html>
<head>
<title> boilerplate </title>
<link rel="stylesheet" href="style.css"/>
<style>
body{
  padding-top: 5rem;
}
</style>

</head>
<body>
  <nav class="navbar navbar-fixed-top navbar-dark bg-inverse">
    <a class = "navbar-brand" href="#"> Demo wertc </a>
  </nav>
  <div class = "container">
    <div class = "row">
      <div class="col-sm-6">
        <h2>Reception</h2>
        <video id="receiver-video" width="100%"height="400px"></video>
<p>
<button id="start">start the connection </button>
</p>

      </div>
      <div class="col-sm-6">
        <h2>Envoi</h2>
        <video id="emitter-video" width="100%"height="400px"></video>
      </div>

    </div>
<script src= "app.js"></script>
  </body>
</html>

app.js

 document.querySelector('#start').addEventListener('click',function(e){
navigator.getUserMedia({
video: true,
audio: true

},function (stream) {

let emitterVideo = document.querySelector('#emitter-video')
emitterVideo.src=window.URL.createObjectURL(stream)
emitterVideo.play()

}, function () {

})

})

Answer №1

  1. When loading the index.html file content using the browser.loadHTML() method, the .css and .js files are not loaded due to their relative path inclusion. To load an HTML file from the local file system, you can use the browser.loadURL() method like this:

    browser.loadURL("file:///D:/media/html/index.html");
    
  2. JxBrowser relies on the Chromium engine, which restricts access to system media devices by JavaScript when running local HTML files. To enable access to these devices from local files, you must disable web security with the following Chromium switch:

    BrowserPreferences.setChromiumSwitches("--disable-web-security");
    

    It is important to note that this method should be called before creating any Browser instance.

  3. If you are attempting to utilize jQuery functionality but it is not included in your HTML file, consider either adding jQuery or implementing your logic using pure JavaScript instead.

Additionally, I suggest exploring the Remote Debugging Port feature, which allows you to debug loaded web pages using Chromium DevTools.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring package.json: Uncovering the Installed Components of a Module

I am working on a project with the following structure: __tests__ example src .bablerc .eslintignore .eslintrd .gitignore package.json package-lock.json README.md The package.json file has the following parameters: { "name": "", "version": "0.0.1", ...

When executed through nodeJS using the `require('./main.ts')` command, TypeScript Express encountered an issue with exporting and displayed undefined

Describing my issue is proving to be challenging, so I have simplified the code. Let me share the code below: main.ts import express from 'express'; let a = 1 console.log ('a in main.ts', a) export let b = a const app = express() let ...

Verifying Value Equality in all Documents with MongoDB

One feature on my website allows users to input a number into the field labeled subNum in a form. Upon submission of the form, I need to validate whether the entered value already exists within any existing document. This validation process is implemented ...

What steps can be taken in Angular to eliminate an additional HTML tag created when using dynamic components with a structural directive?

I currently have a complex infrastructure within my project that includes: host component structural directive used in the host component's template (MyDir) another component used in the structural directive (MyComp) A simplified version is outline ...

What is the most effective method in Vue.js for transitioning elements without the need to use v-for and v-if at the

Utilizing v-for to generate multiple <p> elements, each one animating in within a Vue <transition> component if the v-if condition is met. Below is the code snippet: <transition name="fade"> <p v-for="(quote, idx) in game.quot ...

Transforming the storage mechanism within Redux

I am looking to modify the overall state of Redux (commonly referred to as storage). Below is the code I have written: reducer export const user = (state = {}, action) => { console.log(4); console.log(action.type) console.log(action.payloa ...

"What is the best way to use JavaScript to dynamically modify the hover color of a button

I am looking to customize the hover color of all buttons on my website to align with the overall theme. The challenge is that I need the hover color to vary based on the page it originates from. I have successfully identified the referring page, but I am s ...

Access cookie data using JavaScript or jQuery

One of my tasks is to include an asp page inside an iframe. You can view the page here: If a customer selects a font and then chooses a clip art image or uploads their own image for the clip art, I need to store all those values in cookies. Now, my goal ...

What is causing this issue with the ajax call not functioning correctly?

$(document).ready(function(){ $('.clickthetext').click(function(){ $.post("submit.php", $("#formbox").serialize(), function(response) { $('#content').html(response); }); return false; }); ...

What are the steps to ensure Submit() functions properly?

Two links on a jsp page share a common javascript function func1 as the handler for their onclick events. func1 submits a form that contains both links, resulting in a redirect to a servlet. func1 is defined as: function func1(someValue, form) { getEle ...

Switch background and disable hover effect with a click

I'm facing some issues with JavaScript and I can't seem to solve the problem on my own. What I want is, when I click on #footerblock, for the background of #footerblock to change, and then for the footer to lose its "hover effect". <script> ...

Can Node.js Express be used to export routes for organizing?

Using express.Router() to handle API requests in our application: var express = require('express'); var app = express(); var router = express.Router(); router.use to log messages before each API call: router.use(function(req, res, next) { ...

React - What is the best approach to handling the state of numerous dynamically generated controlled inputs?

Currently, I am iterating through a returned user object and generating a form for each user. Each form is meant to capture 'Add Hours Worked' input data for the specific user. Here's a snippet of the returned user array: employees: [ ...

When using child_process to run a Python script in Node.js, a promise may not resolve properly if the process exits before all

Currently, I am facing an issue while trying to execute sublist3r within a node app. The script runs successfully but only displays the banner before abruptly exiting after about 5 seconds. Typically, the script should interact with the web and take approx ...

I'm encountering an issue where the array is coming back empty after adding elements from JSON. Any tips on how to fix

I am currently working with a .JSON file where I have read the data and implemented a for...in loop to iterate through it. After using console.log, I noticed that while I can view the data in the console, I am unable to add any data to an array using the ...

Exploring the basics of Three.js: a step-by-step guide using a shadertoy example on transforming patterns with objects

Check out this snippet, inspired by the second live example found at : html, body { height: 100%; margin: 0; } #c { width: 100%; height: 100%; display: block; } <canvas id="c"></canvas> <script type="module"> // Three.j ...

Node-fetch enables dynamic requests

Seeking to retrieve real-time data from a fast-updating API has posed a challenge for me. The issue lies in my code constantly returning the same value. I've experimented with two approaches: var fetch = require("node-fetch"); for(let i=0; i<5; i+ ...

Creating an If statement tailored for a particular image source: a step-by-step guide

In the program I am running in Dreamweaver, there is a specific line of code that looks like this: function go() { if((document.test.test1.src == document.test.test2.src && document.test.test2.src == document.test.test3.src )) ...... ...... ..... ...

What is the best way to integrate a React component into a Data Grid implemented with Material-UI upon button click?

How can I achieve the functionality of opening a pop-up-like component 'ViewDownload.js' upon clicking the Download Button in a cell of the Data Grid within Material-UI? I want the click action on the Download button to trigger the display of a ...

Is there a way to store the outcome of my node api request in a variable and then transmit it using Express?

Currently, I am leveraging Node to initiate an API call to a movie database. This Node API call is enclosed within an Express route. // Mandatory module requirements var express = require('express'); var router = require('./router.js&ap ...