Thursday, 25 February 2016

How to play videos in JAVA

In previous articles i showed you that JAVA having broad working areas. Today i will show you how to play videos in JAVA using javafx api.
For playing videos in JAVA we need following things :
  • JAVA 7 JDK or greater
  • Eclipse or Netbeans IDE 
Install all of these in your system. After that start eclipse IDE and make a JAVA project. After it add a class file named as you want.  Now make the following program. Here i am showing you a simple MP4 video play without any other controls on media player window.

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;



public class video extends Application{

    private Media media;
    private MediaPlayer player;
    private MediaView view;
    private Scene scene;
    private Group root;
    public void start(Stage stage) throws Exception {
        root = new Group();
        scene = new Scene(root,800,600);
        media = new Media(new File("/media/amar/Disk 3/my softwares/dhoom3.mp4")     

        player = new MediaPlayer(media);;

        view = new MediaView(player);


        stage = new Stage();

After it save the program and run as JAVA application. You will see the video is playing normally. The drawback of media player in JAVA that it is unable to support all video formats. 
Good Luck guys.