How to make a standard video player in Java

It is quite difficult even to make a simple video player in Java. This requires at least a basic understanding of language syntax, ability to work with frameworks and debug. A very important piece of solving the problem is finding bugs in the program.

It is enough easy to create a program in Java. But it is the more difficult task to make it work, and  work well, quickly and correctly.

So, how to make a media player using Java?

There is a lot of tools for the realization of video player using Java. For example, using the FFmpeg library for Java with codecs in it, JMF (an acronym for Java Media Framework), Native Swing, MediaPlayer class for Android, VLCJ (Java Bindings for VideoLAN) and others.

Consider the realization of the video player in Java using Java Media Framework.

By itself, JMF cannot play modern video. It needs a plugin which would recognize different formats. It is actually useless without them.

There were many enthusiasts who have added these plugins. But after a time, support and upgrades for those plugins became impossible or were almost abandoned.

  • JMF (stable)
  • License: SCSL
  • Compatibility: All java-supported platforms
  • Cross-platform: Yes

Downloading JMF is represented by two varieties: dependent platform and cross-platform version. Sun company provides platform-specific versions for Solaris and Windows. (Also available Linux version from Blackdown.)

You may find JMF API on Oracle’s site or your favorite search engine.

There are 4 jar files in that archive that will need to be added to the project.

Few words about IDE

[announce]

“How to make a video player in Java NetBeans?” is an enough easy question. Actually, it’s the same as creating it in IntelliJ IDEA or, for example, Eclipse.

In our case, it will be Eclipse.

It`s easy if you work in Eclipse, so to add files to your project, just click on the right button on the project name and then choose Build Path-Configure Build Path-Add External JARs.

Playing multimedia files using JMF is a simple operation. Key classes are Manager and Player. The Manager has a set of methods createPlayer(), each returning Player. After creating a player you tell it to start playback.

The Player is a type of Controller, and the controllers allow you to register a ControllerListener. ControllerListener contains a single method: public void controllerUpdate(ControllerEvent event). This method is used to intercept various events with media data, such as reaching the end of the video data, completing the download, the beginning and end of playback of media data.

The answer to the question “How to make a video player using Java?” is in this code project:

package example;

import java.awt.Dimension;
import javax.swing.*;
import javax.media.bean.playerbean.MediaPlayer;

class VideoExample extends JFrame{
MediaPlayer player;//our player

public VideoExample(String path){
super("Simple video player");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(new Dimension(640,480));//set the window size

player = new MediaPlayer();
//path – path to the file
player.setMediaLocation("file:///" + path); 
player.setPlaybackLoop(false);//repeat the video
player.prefetch ();//preliminary processing of the player (without it the player will not appear)
//add to the frame 
add(player);
//player.start (); - start the player immediately

setVisible(true);
}

public static void main(String []args){
VideoExample ve = new VideoExample("D:\\1.mpeg");
}
}

Everything seems to be fine. We started the application.  Find the control panel and push the play button.

But here comes the problem. The documentation for it says that the Java Media Framework supports such common formats as Avi and MPEG. This is not true.

The video does not show any visual images at all or you can hear only the audio part.

What to do now?

You should add the library jffmpeg-1.1.0.jar  which includes the codecs for playing video and audio formats to the project to solve the problem.

Now you know for certain how to make a simple video player in Java. So, you may try.

But first of all, think about this.

Do you know enough Java specification to create full wholesome media player?

Or do you know how to make well-designed video player in JavaFX, which will meet absolutely all your requirements and quality standard?

You may just give this task to the professional developers from Letzgro: http://letzgro.net/contacts/

They know what you need.

Tags: Java player video

Insights from our Consulting Department

January 22, 2019
How To Convert Traffic into Sales with Lead Conversion Strategy
October 15, 2016
How to Create the Smartest Chatbot Ever With IBM Watson Services

Leave a Reply

Your email address will not be published. Required fields are marked *