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
  • JAVAFX SDK
  • 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.*;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

import java.io.*;

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();
        root.setAutoSizeChildren(true);
        scene = new Scene(root,800,600);
   
        media = new Media(new File("/media/amar/Disk 3/my softwares/dhoom3.mp4")     
       
.toURI().toString());

        player = new MediaPlayer(media);
        player.play();


        view = new MediaView(player);
        view.fitHeightProperty().bind(scene.heightProperty());
        view.fitWidthProperty().bind(scene.widthProperty());


        root.getChildren().add(view);

        root.getChildren().add(vol);
        stage = new Stage();
        stage.setScene(scene);
        stage.show();
      
    }
   
 


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.

No comments:

Post a Comment