How to play a sound in a JSF web app? - java

Is it possible to play a sound (.wav or .mp3) in a client's browser using a JSF web app?
I have tried using javax.sound.sampled.SourceDataLine and it worked on Windows, but when I deploy the .war on a Linux host I get this exception:
"javax.sound.sampled.LineUnavailableException: Audio Device Unavailable"

Java code executes on the server to generate web pages that are displayed in the client's browser. If you have JSF web app code using the javax.sound.sampled.SourceDataLine class, the sound will play on the server (if this is even possible...), not in the client's browser.
To play a sound at the client, you'll either have to reference an audio file in the web page as Pascal Thivent has posted, or you'll have to play the sound through a Flash/Silverlight/Java Applet plugin referenced in the page.

Is it possible to play a sound (.wav or .mp3) in a client's browser using a JSF web app?
I should have mentioned that, but this has nothing to do with JSF or any other server-side technology. If you want to play a sound on the client-side, generate the appropriate client-side code, i.e. HTML here.
HTML 5 has an <audio> tag but, until it gets mainstream, see Playing Sounds on a Web Site and How To Play Sound (which does a good job at summarizing the solutions) to learn more on the <embed> tag and the <object> tag.

I have tried using javax.sound.sampled.SourceDataLine and it worked on Windows, but when I deploy the .war on a Linux host I get this exception: "javax.sound.sampled.LineUnavailableException: Audio Device Unavailable"
This exception literally means that the machine where the code runs doesn't have an audio device. I can imagine that it is very reasonable that webservers doesn't have any audio device, this namely doesn't make any sense.
As others have pointed out, you need to play sound at the client machine. You can find here a lot of background information and examples.

Not via JSF but you can use primefaces for an app like that http://www.primefaces.org/showcase/ui/media.jsf

Related

How to create a desktop app for the existing web application?

Well, I've tried to do some researches before creating a question but only a little has been found.
Basically, I have got a Spring-based web application. Apparently, you can navigate through web application using HTTP requests and URLs. If you want to edit a user you do a GET request /users/edit/{id} and a new page appears. Then you make some changes to the user and do a POST request /users/edit/{id} and let's say the main page appears.
So, now I need to create a desktop application which can do the same things.
Do I need to rewrite the whole app to port it on the desktop? Is it possible somehow to do sort of HTTP requests from the desktop app to the server, then get a response and process it? Or perhaps there is a proper way to do it?
I feel like it's a big topic but I only need you to point me in the right direction as I'm lacking experience in creating both desktop and web applications together (I'd say, I have never ported app from web to desktop and vice verse).
Earlier I created a few apps using JavaFX and I want to use it again as my GUI platform for the desktop app.
You can go for a solution like, Electron.
It's a framework for creating native applications with web technologies like JavaScript, HTML, and CSS. It uses Chromium and Node.js. You can develop your desktop GUI applications using front and back end components originally developed for your web application.
I'm just pointing you a way. You can look in to this solution and it's also open-source.
If you were about to develop the frontend from scratch I'd recommend you to use something like Ionic Framework where in the latest beta 4 allows you to write once run everywhere (web, PWA, desktop with Electron, and build native build for iOS and Android).
Basically Ionic is an Angular 7 library/superset that allows you to create mobile apps based on a webview. This webview can be embedded and run on any device in a native way.
Even though the same codebase can be run on multiple devices you can customise the look and feel on each platform and access through Cordova, Electron or Capacitor the native device capabilities and hardware.
If you don't want to rewrite the web client, you can use any desktop framework with the preferred language you want. Any modern language/framework will allow you to make HTTP requests against your backend and get the data through your API.
Honestly, nowadays I'd day that JavaFX is not the best option out there and I'd recommend you to go through the web-based approach.
Good luck!

What is the alternatives for Java Applet to launch client programs using Chrome browsers?

I am working in a web application using ASP.net and C#.
My target is to check on a desktop application on the client machine .. If it was exist then I should launch it. Else if it wasn't exist, we should download, install and then launch it.
I could develop this module using Java Applet but unfortunately, Google decided to disable NPAPI in September 2015, So the applet will not working on Chrome.
My question is about the Applet alternatives to help me implementing the above scenario ?
The only (semi) viable option as far as I can see, is to offer the Chrome user a link to a JNLP file for a desktop application. Then when/if the JWS desktop application starts, have it report back to the server 'loaded OK' and then 'target app. installed/not installed'.
On your server, have a time out for waiting on the client to report back. If it passes that time, presume the client machine does not support Java at all and the user is checking some web service to try and figure out what app. opens a JNLP!
After searching about the most suitable way to achieve my goal, I think using custom protocols will be a nice solution to access my client applications from the web page.
This solution is used by many companies such as Microsoft (using mailto: to open Outlook application) and Apple (using itms: to open iTunes application).
The following link is a very good link which talk about a custom protocols:
https://support.shotgunsoftware.com/entries/86754-How-to-launch-external-applications-using-custom-protocols-rock-instead-of-http-
Once Google Chrome was the first to announce that they won’t be supporting NPAPI anymore, they were also the first to provide a new architecture in order to rewrite your code to work on their browser. You can take a look on Native Messaging, which “can exchange messages with native applications using an API that is similar to the other message passing APIs”. The problem is that this approach only works on Chrome, is not something that you can adapt to other browsers.
A more useful approach is FireBreath, a browser plugin in a post NPAPI world. Check the words below from one buddy of the project:
“FireBreath 2 will allow you to write a plugin that works in NPAPI, ActiveX, or through Native Messaging; it’s getting close to ready to go into beta. It doesn’t have any kind of real drawing support, but would work for what you describe. The install process is a bit of a pain, but it works. The FireWyrm protocol that the native messaging component uses could be used with any connection that allows passing text data; it should be possible to make it work with js-ctypes on firefox or plausibly WEB-RTC or even CORS AJAX in some way. For now the only thing we needed to solve was Chrome, but we did it in a way that should be pretty portable to other technologies.”

event java google app engine

I just started using google app engine to develop a web application and I'm a little lost on how to deal with events sent from buttons properly.
For some reason, when I try to use a JButton on my application I get this error.
"javax.swing.JButton is not supported by Google App Engine's Java runtime environment"
the only way I managed to get an on_click event so far was to use an html button and use a "post" or "get" requests.
So, is their a tutorial somewhere to help me understand better how to use buttons and other components in my application?
I think you have a deep misunderstanding of what Google AppEngine does. You deploy your code to AppEngine, and it runs on Google's servers. It generally interacts with users in the form of HTTP requests. A user enters a URL, and the Java code might send back some HTML and javascript.
A JButton is a Java Swing component, which is used when developing Java software to run on an end user's machine. Your AppEngine app is not going to run on an end user's machine. You are seeing that error message, because GAE has a whitelist of allowed classes from the JRE.
https://developers.google.com/appengine/docs/java/jrewhitelist
It's possible you may be thinking of Google Web Toolkit(GWT). GWT allows you to write the client/server of a web application in Java, and then cross-compile the client into javascript. This is separate from AppEngine. Although you can develop a GWT application, and deploy it on AppEngine.

Bluetooth from a webpage

Is it possible to have JavaScript which calls a Java file (or something of the sort), so we can connect to a Bluetooth device and send it commands from a web page?
If it is possible, what are some ideas behind it that I can use to get it working?
Sorry for my poor language, I am not really sure how to word it.
Java can be used to send files via Bluetooth, and there is a tutorial on it at http://today.java.net/pub/a/today/2004/07/27/bluetooth.html
You could run a Java applet from the web browser that would use Bluetooth, although it would probably come up with a warning asking if you want to give the applet access to the files on your computer. JavaScript, however, would not be necessary, since an applet can run directly on a webpage.

How to make a P2P streaming using a web browser?

I want to create an application for P2P video transmission. In more details, I need the following:
User can switch on a web camera by clicking a button in an application.
Application starts to take images from the web camera and sends them to a certain location (given by a port and IP address).
Application starts to accept images send by another application and display them (images) in a window.
There is also one more thing. I would prefer to have everything in a browser. I know that everything can be done with Flash Player from Adobe (an example is http://chatroulette.com/).
But I would like to know if the same can be done with JavaScripts.
I am sure it the application can be written in Java. But can I use Java to have everything in a Browser.
I'm not sure that webcam access is available in any of the browsers natively yet so you might be out of luck for a pure js solution. I think there's meant to be something added to the spec soon though.
If you want to do it with flash (which is really the best way at present) take a look at red5 which you can run on the server to sit between the clients and deal with the video streaming.

Categories

Resources