Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx MediaPlayer to play partially downloaded Media file (TCP/UDP, not HTTP)

Javafx MediaPlayer class plays files loaded into a Media object. The Media object can load local Media files or via HTTP.

My Client-Server uses TCP/UDP, not HTTP. I assume that media files need to be downloaded locally to start playback. I need to know if playback can start when media is partially loaded.

Can MediaPlayer start local playback of a partially downloaded Media file and how many bytes need to be downloaded to start playback, or the Media file needs to be fully downloaded?

I'm looking for general advice.

like image 255
Evgeny Avatar asked Oct 15 '25 03:10

Evgeny


2 Answers

The Media documentation clarifies that all the information must be available for it to be played by the MediaPayer:

The media information is obtained asynchronously and so not necessarily available immediately after instantiation of the class. All information should however be available if the instance has been associated with a MediaPlayer and that player has transitioned toMediaPlayer.Status.READY status.

considering that the READY status is a prerequisite for playing. So playing partially loaded medias is not possible, despite the loading being asynchronous.

Also, the Media constructor takes only an URI parameter:

Only HTTP, FILE, and JAR URLs are supported. If the provided URL is invalid then an exception will be thrown.

This does allow to specify an address and a port, but does not allow to specify the use of TCP/UDP rather than http.

like image 98
Christophe Avatar answered Oct 18 '25 04:10

Christophe


I assume that media files need to be downloaded locally to start playback

No. Complete download of a media file before playback is not necessarily required.

Media can be streamed, without local download, as long as that is done using a supported format and protocol.

Supported formats and protcols are documented in the JavaFX Media Documentation.

Currently the only streaming format supported for JavaFX 24 is Http Live Streaming (HLS).

But also, HTTP direct from a hosted file on a URL (e.g. an MP4 or MP3 served by a standard web server using the HTTP protocol) will also be supported (by buffering chunks of data to be played), but probably with a bit less performance and functionality than a HLS stream.

Client-Server uses TCP/UDP, not HTTP.

Technically, HTTP is an application protocol that runs over a transmission protocol (TCP), so a request via HTTP will also use TCP, but not necessarily vice-versa.

I guess that your source uses some other protocol that runs over TCP/UDP, for example:

  • RTP

OR

  • WebRTC

Neither of which is directly supported by JavaFX 24 Media.

Options outside of JavaFX Media

Your options are to use a 3rd party component to either:

  • Convert the incoming stream to another streaming format that JavaFX Media can use.

OR

  • Consume and handle the incoming stream outside of JavaFX Media.

You could use a Java utility that can handle the incoming stream and render the output using JavaFX without using JavaFX media (either rendering to a JavaFX WriteableImage or to a Swing/AWT canvas via JavaFX's Swing integration).

Recommending 3rd party libraries is out of scope for StackOverflow, but I'll list some approaches to give you some idea of what is required.

Stream conversion to HLS

Tools such as FFMPEG are capable of transforming an incoming media stream, for example from an IP camera, to a HLS format. JavaFX Media would likely be able to render the transformed stream.

Playback of RTSP with JavaFX

vlcj-javafx can handle playback of RTSP streams in JavaFX. Video is rendered in a JavaFX WriteableImage and the decoding is handling by VLC installed on the machine.

Displaying RTP MJPEG

If you want to write your own protcol handler and renderer to render into JavaFX (not recommended), here is an example:

  • Display RTP MJPEG

One advantage of this approach is that the decoding and rendering is handled completely in Java/JavaFX and no additional 3rd party native components (such as FFMPEG or VLC) need to be installed.

like image 24
jewelsea Avatar answered Oct 18 '25 03:10

jewelsea