Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating video player using Java

I need to create a video player using Java for my project. I have already checked many examples on the Internet. Some of them run, but do not show any screen, I can only hear the sound of the video. Please help me solve this...

I am using the below import

import javax.media.*;

EDIT: Below is the code that i use:

import java.awt.*;
 import java.awt.event.*;
 import java.io.*;
 import javax.swing.*;
 import javax.media.*;

 public class MediaPlayerDemo extends JFrame 
 {
     private Player player;
     private File file;

     public MediaPlayerDemo()
     {
         super( "Demonstrating the Java Media Player" );

         JButton openFile = new JButton( "Open file to play" );
         openFile.addActionListener( new ActionListener() 
         {
             public void actionPerformed( ActionEvent e )
             {
                 openFile();
                 createPlayer();
             }
         });
         getContentPane().add( openFile, BorderLayout.NORTH );

         setSize( 300, 300 );
         show();
     }

     private void openFile()
     {
         JFileChooser fileChooser = new JFileChooser();

         fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
         int result = fileChooser.showOpenDialog( this );

         // user clicked Cancel button on dialog
         if ( result == JFileChooser.CANCEL_OPTION )
             file = null;
         else
             file = fileChooser.getSelectedFile();
     }

     private void createPlayer()
     {
         if ( file == null )
             return;

         removePreviousPlayer();

         try 
         {
             // create a new player and add listener
             player = Manager.createPlayer( file.toURL() );
             player.addControllerListener( new EventHandler() );
             player.start(); // start player
         }
         catch ( Exception e )
         {
             JOptionPane.showMessageDialog( this, "Invalid file or location", "Error loading file",
             JOptionPane.ERROR_MESSAGE );
         }
     }

     private void removePreviousPlayer()
     {
         if ( player == null )
             return;

         player.close();

         Component visual = player.getVisualComponent();
         Component control = player.getControlPanelComponent();

         Container c = getContentPane();

         if ( visual != null )
             c.remove( visual );

         if ( control != null )
             c.remove( control );
     }

     public static void main(String args[])
     {
         MediaPlayerDemo app = new MediaPlayerDemo();

         app.addWindowListener( new WindowAdapter() 
         {
             public void windowClosing( WindowEvent e )
             {
                 System.exit(0);
             }
         });
     }

     // inner class to handler events from media player
     private class EventHandler implements ControllerListener 
     {
         public void controllerUpdate( ControllerEvent e ) 
         {
             if ( e instanceof RealizeCompleteEvent ) 
             {
                 Container c = getContentPane();

                 // load Visual and Control components if they exist
                 Component visualComponent = player.getVisualComponent();

                 if ( visualComponent != null )
                     c.add( visualComponent, BorderLayout.CENTER );

                 Component controlsComponent = player.getControlPanelComponent();

                 if ( controlsComponent != null )
                     c.add( controlsComponent, BorderLayout.SOUTH );

                 c.doLayout();
             }
         }
     }
 }
like image 345
Anna Avatar asked Dec 02 '15 08:12

Anna


2 Answers

I used vlcj and it worked smoothly. It's java binding to vlcj player and the good thing that you don't have to provide any drives since vlcj already includes all of them in binary distribution.

Give it a go, there is example of already working player built for you!

like image 199
Petro Semeniuk Avatar answered Oct 10 '22 12:10

Petro Semeniuk


Try a JavaFX Mediaplayer:

Usable Codecs:

  • Audio: MP3; AIFF containing uncompressed PCM; WAV containing uncompressed PCM; MPEG-4 multimedia container with Advanced Audio Coding (AAC) audio
  • Video: FLV containing VP6 video and MP3 audio; MPEG-4 multimedia container with H.264/AVC (Advanced Video Coding) video compression .

Here an example from Oracle:

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.collections.MapChangeListener;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaView;
import javafx.scene.media.Track;
import javafx.stage.Stage;

/**
 * A sample media player which loops indefinitely over the same video
 */
public class MediaPlayer extends Application {
private static final String MEDIA_URL = "http://someserver/somedir/somefile.mp4";

private static String arg1;

    @Override public void start(Stage stage) {
        stage.setTitle("Media Player");

// Create media player
        Media media = new Media((arg1 != null) ? arg1 : MEDIA_URL);
        javafx.scene.media.MediaPlayer mediaPlayer = new javafx.scene.media.MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE);

// Print track and metadata information
        media.getTracks().addListener(new ListChangeListener<Track>() {
public void onChanged(Change<? extends Track> change) {
                System.out.println("Track> "+change.getList());
            }
        });
        media.getMetadata().addListener(new MapChangeListener<String,Object>() {
public void onChanged(MapChangeListener.Change<? extends String, ? extends Object> change) {
                System.out.println("Metadata> "+change.getKey()+" -> "+change.getValueAdded());
            }
        });

// Add media display node to the scene graph
        MediaView mediaView = new MediaView(mediaPlayer);
        Group root = new Group();
        Scene scene = new Scene(root,800,600);
        root.getChildren().add(mediaView);
        stage.setScene(scene);
        stage.show();
    }

public static void main(String[] args) {
if (args.length > 0) {
            arg1 = args[0];
        }
        Application.launch(args);
    }
}

https://blogs.oracle.com/javafx/entry/mpeg_4_multimedia_support_in

like image 32
Stefan Sprenger Avatar answered Oct 10 '22 12:10

Stefan Sprenger