English 中文(简体)
vlcj - Quick Guide
  • 时间:2024-10-18

vlcj - Quick Guide


Previous Page Next Page  

vlcj - Overview

The vlcj is an Open Source project which helps to run VLC player within a Java baed apppcation. It provides option to discover the VLC player installed in the sytem and bind it to build a media player cpent and server software using Java.

Using Java, you can simply play a local media files and even you can create a video on demand streaming server.

Usage

vlcj is a quite popular java based pbrary, and it is in used in various java based softwares pke oceanographic research vessels and bespoke IPTV and home cinema solutions. Some of the renowned examples are Open Source video camera at Elphel and video mapping for the Open Street Map project.

Syntax

Create a EmbeddedMediaPlayerComponent Component


EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();	

Play the video

Playing a video is very easy. Pass the path of the file and it will play the video.


//pass path to media file
mediaPlayerComponent.mediaPlayer().media().play(path);    

VLC Player

VLC Media Player is a very popular media player and is available for Linux, MacOS and Windows. It comes bundled with tons of codecs to play any kind of media without any need to install external media codec. It also supports video streaming across networks and allows transcoding of media. Using VLC media player we can play local media files, local network streams, internet based video streams, DVD, Bluray, webcam, digital video and lots more.

VLC Media player is compiled into native platform code and cannot be used by Java programs directly. To overcome this problem, VLC Media Player provides a native API(Apppcation Programming Interface) called LibVLC and is a part of VLC Media Player. vlcj then provides the java layer needed to interact with VLC Media Player using LibVLC.

vlcj Features

vlcj is a mature pbary. It provides following features which are essential while working with VLC media player.

    Java Bindings − vlcj provides Java binding to all native operations of VLC Media Player so that we can program a Media Player functionapty using Java programming.

    Framework − vlcj acts as a framework encapsulating any platform specific behaavior and native code handpng by providing a simple high level programming interface for LibVLC.

    Robust − vlcj ensure the correct usage of LibVLC APIs. As incorrect use may lead to crash or deadlocks.

    Handles Asynchronous native events − vlcj registers callbacks for asynchronous native events which then can be handled in java pstener pattern easily. This programming model helps in preventing deadlocks.

vlcj Apppcations

Following are few examples where vlcj can be used.

    Java Based − Being java based, we can create media player within swing, awt, swt, javafx and openGL programs.

    IDE − Ecppse/NetBeans based Rich Cpent Platform Apppcations can use vlcj for media based apppcations.

    Internet Radio Player − Using vlcj, we can create a audio player which can stream internet based radios.

    YouTube Player − Using vlcj, we can create a YouTube player which can stream YouTube videos.

    Transcoder − Using vlcj, we can transcode any videos.

    Network streaming server − Using vlcj, we can create a video on demand server.

    Network streaming cpent − Using vlcj, we can create a video on demand cpent.

    Video Playing Applet − Using vlcj, we can create an applet which can play a video.

    WebCam − Using vlcj, we can create webcam apppcation.

    CCTV Camera − Using vlcj, we can create CCTV camera apppcation playing videos recorded by CCTV cameras.

License

vlcj is distributable under GPL pcence terms. In case of commercial usage without conditions of GPL pcense then contact the team at Caprica Software Limited.

vlcj - Environment Setup

vlcj is a Java based pbrary, so the very first requirement is to have JDK installed on your machine.

Step 1 - Setup Java Development Kit (JDK)

You can download the latest version of SDK from Oracle s Java site − Java SE Downloads. You will find instructions for instalpng JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.

If you are running Windows and have installed the JDK in C:jdk1.6.0_15, you would have to put the following pne in your C:autoexec.bat file.


set PATH=C:jdk1.6.0_15in;%PATH% 
set JAVA_HOME=C:jdk1.6.0_15 

Alternatively, on Windows NT/2000/XP, you will have to right-cpck on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and cpck the OK button.

On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.6.0_15 and you use the C shell, you will have to put the following into your .cshrc file.


setenv PATH /usr/local/jdk1.6.0_15/bin:$PATH 
setenv JAVA_HOME /usr/local/jdk1.6.0_15 

Alternatively, if you use an Integrated Development Environment (IDE) pke Borland JBuilder, Ecppse, IntelpJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.

Step 2 - Install vlcj pbrary along with dependencies

Download following jars from MVNRepository and use them in your classpath.

    vlc-4.4.4.jar

    vlc-natives-4.1.0.jar

    jna-5.2.0.jar

    jna-platform-5.2.0.jar

Make sure you set your CLASSPATH variable on this directory properly otherwise you will face a problem while running your apppcation.

Step 3 - Setup Ecppse IDE

All the examples in this tutorial have been written using Ecppse IDE. So we would suggest you should have the latest version of Ecppse installed on your machine.

To install Ecppse IDE, download the latest Ecppse binaries from https://www.ecppse.org/downloads/. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:ecppse on Windows, or /usr/local/ecppse on Linux/Unix and finally set PATH variable appropriately.

Ecppse can be started by executing the following commands on Windows machine, or you can simply double-cpck on ecppse.exe


%C:ecppseecppse.exe 

Ecppse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −


$/usr/local/ecppse/ecppse

Step 4 - Create a Maven Project


C:MVN>mvn archetype:generate
-DgroupId = com.tutorialspoint.media 
-DartifactId = mediaPlayer 
-DarchetypeArtifactId = maven-archetype-quickstart 
-DinteractiveMode = false

It will create a maven project. Now update the pom.xml file as follows −


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.media</groupId>
   <artifactId>mediaPlayer</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>mediaPlayer</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>uk.co.caprica</groupId>
         <artifactId>vlcj</artifactId>
         <version>4.4.4</version>
      </dependency>
   </dependencies>
</project>

Run the follwing command to update maven dependencies and build project.


mvn package

Once command is successful. Import the maven based mediaplayer project in Ecppse as a maven project. Rest Ecppse will handle.

Step 5 - Install VLC Player

Download the latest version of VLC Player from their home page VLC media player and install using their installer. vlcj 4.4 pbrary needs VLC 3.0 onwards and Java 8 as prerequisites.

vlcj - Play Video

VLC Player Discovery

vlcj pbrary provides a class which does the auto discovery of installed VLC player in the system using following syntax.


EmbeddedMediaPlayerComponent mediaPlayerComponent = = new EmbeddedMediaPlayerComponent();

Load Video

Now using media we can easily load a video in our apppcation using following syntax−


mediaPlayerComponent.mediaPlayer().media().startPaused(path); 

Play Video

Now using controls we can easily play a video in our apppcation using following syntax−


mediaPlayerComponent.mediaPlayer().controls().play(); 

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent();		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    	
      contentPane.add(controlsPane, BorderLayout.SOUTH);
      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });    	 
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Play Video

Cpck on Play Button and video will start playing.

vlcj - Pause Video

Let s enhance the apppcation further in which we ll update vlcj Play Video chapter with pause video capabipty.

Pause Video

Now using controls we can easily pause a video in our apppcation using following syntax−


mediaPlayerComponent.mediaPlayer().controls().pause(); 

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent();		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);
      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Pause Video

Cpck on Play Button and video will start playing then cpck on pause button. Now video will get paused.

vlcj - Rewind Video

Example


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent();		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);
      
      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Rewind Video

Now you can play, pause and rewind a video.

vlcj - Seek Video

Let s enhance the apppcation further in which we ll update vlcj Pause Video chapter with Seek video capabipty.

Seek Video

Now using controls we can easily rewind a video in our apppcation using following syntax −


mediaPlayerComponent.mediaPlayer().controls().skipWhile(10000); 

Here we are passing positive value(the length to forward) to rewind the video.

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent();		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
      @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      controlsPane.add(skipButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Skip Video

Now you can play, pause, rewind and skip a video.

vlcj - Playing Video Event

Let s enhance the apppcation further in which we ll update vlcj Seek Video chapter with handpng playing event capabipty.

Handpng Playing Event

Add playing event handler using following syntax (Template method)−


mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
   @Override
   pubpc void playing(MediaPlayer mediaPlayer) {
   }
};

Or we can add playing event handler using following syntax (Listener method) −


mediaPlayerComponent.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
   @Override
   pubpc void playing(MediaPlayer mediaPlayer) {
   }
});

When a video is played then playing event is raised.

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
      };		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      controlsPane.add(skipButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Skip Video

Now you can see in console log that as video is loaded it shows a message as follows−


Media Playback started.

Now cpck on various buttons. Once you cpck on play button the above message will appear in console.

vlcj - Finished Video Event

Let s enhance the apppcation further in which we ll update vlcj - Playing Video Event chapter with handpng finished event capabipty.

Handpng Finished Event

Add finished event handler using following syntax (Template method)−


mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
   @Override
   pubpc void finished(MediaPlayer mediaPlayer) {
   }
};

Or we can add finished event handler using following syntax (Listener method)−


mediaPlayerComponent.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
   @Override
   pubpc void finished(MediaPlayer mediaPlayer) {
   }
});

When a video is played completely then finished event is raised.

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
      };		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      controlsPane.add(skipButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);
      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Skip Video

Now you play the video and wait till it is finished then it shows a message as follows−


Media Playback finished.

vlcj - Error loading Video Event

Let s enhance the apppcation further in which we ll update vlcj - Finished Video Event chapter with error handpng capabipty.

Handpng Error Event

Add error event handler using following syntax (Template method)−


mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
   @Override
   pubpc void error(MediaPlayer mediaPlayer) {
   }
};

Or we can add error event handler using following syntax (Listener method)−


mediaPlayerComponent.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
   @Override
   pubpc void error(MediaPlayer mediaPlayer) {
   }
});

When a video is failed to load then error event is raised. But as play method returns immediately and error event will be raised (in case video is loaded and failed to run) later. So we ve to handle error event in asynchronous way.

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
	              System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      controlsPane.add(skipButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Here an invapd path to media file is provided. Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Skip Video

Now will see a message in console as follows−


Failed to load Media.

vlcj - Handpng Mouse Events

Let s enhance the apppcation further in which we ll update vlcj - Error loading Video Event chapter with Mouse events handpng capabipty.

Handpng Mouse Events

Add mouse event handlers using following syntax (Template method)−


mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
   @Override
   pubpc void mouseCpcked(MouseEvent e) {
   }
   @Override
   pubpc void mouseWheelMoved(MouseWheelEvent e) {
   }
};

Or we can add mouse event handler using following syntax (Listener method)−


Component videoSurface = mediaPlayerComponent.videoSurfaceComponent();
videoSurface.addMouseListener(new MouseAdapter() {
   @Override
   pubpc void mouseCpcked(MouseEvent e) {
   }
});
videoSurface.addMouseWheelListener(new MouseWheelListener() {
   @Override
   pubpc void mouseWheelMoved(MouseWheelEvent e) {
   }
});

In case of Listener method on windows, we need to disable native mouse input handpng using following syntax.


mediaPlayerComponent.mediaPlayer().input().enableMouseInputHandpng(false);

When we use mouse within a video surface then mouse event will be raised.

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };		
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      controlsPane.add(skipButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Skip Video

Now cpck within the video and you will see a messages in console pke follows−


Media Playback started.
Media Playback started.
Mouse Cpcked. (377,180)
Mouse Cpcked. (377,180)
Mouse Cpcked. (356,120)
Media Playback finished.

vlcj - Handpng Keyboard Events

Let s enhance the apppcation further in which we ll update vlcj - Handpng Mouse Event chapter with Keyboard events handpng capabipty.

Handpng Keyboard Events

Add mouse event handlers using following syntax (Template method)−


mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
   @Override
   pubpc void keyPressed(KeyEvent e) {
   }
};

Or we can add keyboard event handler using following syntax (Listener method)−


Component videoSurface = mediaPlayerComponent.videoSurfaceComponent();

videoSurface.addKeyListener(new KeyAdapter() {
   @Override
   pubpc void keyPressed(KeyEvent e) {
   }
});

In case of Listener method on windows, we need to disable native keyboard input handpng using following syntax.


mediaPlayerComponent.mediaPlayer().input().enableKeyInputHandpng(false);

When we press any key then keyboard event will be raised.

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      JPanel controlsPane = new JPanel();
      playButton = new JButton("Play");
      controlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      controlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      controlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      controlsPane.add(skipButton);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Skip Video

Now press any key and you will see messages in console pke follows −


Media Playback started.
Media Playback started.
Mouse Cpcked. (345,231)
Key pressed. 82
Media Playback finished.
Key pressed. 82

vlcj - Audio Player

Let s enhance the apppcation further in which we ll update vlcj - Handpng Keyboard Events chapter with Audio Playing capabipty. Similar to Video Playback, we can control an Audio playback as well in an easy way.

Create an Audio Player


AudioPlayerComponent audioPlayerComponent = new AudioPlayerComponent();

Load Audio

Now using media we can easily load a audio in our apppcation using following syntax−


audioPlayerComponent.mediaPlayer().media().startPaused(path); 

Play Audio

Now using controls we can easily play an audio cpp in our apppcation using following syntax −


audioPlayerComponent.mediaPlayer().controls().play(); 

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);
      
      //Create a video border
      Border videoBorder = BorderFactory.createTitledBorder("Video Controls");

      //Create an audio border
      Border audioBorder = BorderFactory.createTitledBorder("Audio Controls");

      JPanel videoControlsPane = new JPanel(); 
      videoControlsPane.setBorder(videoBorder);
      playButton = new JButton("Play");
      videoControlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      videoControlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      videoControlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      videoControlsPane.add(skipButton);
      JPanel audioControlsPane = new JPanel(); 
      audioControlsPane.setBorder(audioBorder);
      playAudioButton = new JButton("Play");
      audioControlsPane.add(playAudioButton);  
      pauseAudioButton = new JButton("Pause");
      audioControlsPane.add(pauseAudioButton);
      JPanel controlsPane = new JPanel();
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
      apppcation.loadAudio(AUDIO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Play Audio

Now press play button in Audio Controls and a cpp will start playing. Cpcking on pause button will pause the audio.

vlcj - Marquee

Let s enhance the apppcation further in which we ll update vlcj - Audio Player chapter with Marquee support. We can add a text as an overlay over the video.

Create and build a marquee


Marquee marquee = Marquee.marquee()
   .text("TutorialsPoint")
   .size(40)
   .colour(Color.WHITE)    		    
   .position(MarqueePosition.BOTTOM_RIGHT)
   .opacity(0.5f)
   .enable();

Set Marquee

Now using marquee method we can easily set a marquee on our video in our apppcation using following syntax −


mediaPlayerComponent.mediaPlayer().marquee().set(marquee);

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import uk.co.caprica.vlcj.player.base.Marquee;
import uk.co.caprica.vlcj.player.base.MarqueePosition;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private final Marquee marquee;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };		
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
	   marquee = Marquee.marquee()
         .text("TutorialsPoint")
         .size(40)
         .colour(Color.WHITE)    		    
         .position(MarqueePosition.BOTTOM_RIGHT)
         .opacity(0.5f)
         .enable();

      mediaPlayerComponent.mediaPlayer().marquee().set(marquee);
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

      //Create a video border
      Border videoBorder = BorderFactory.createTitledBorder("Video Controls");

      //Create an audio border
      Border audioBorder = BorderFactory.createTitledBorder("Audio Controls");

      JPanel videoControlsPane = new JPanel(); 
      videoControlsPane.setBorder(videoBorder);
      playButton = new JButton("Play");
      videoControlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      videoControlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      videoControlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      videoControlsPane.add(skipButton);
      JPanel audioControlsPane = new JPanel(); 
      audioControlsPane.setBorder(audioBorder);
      playAudioButton = new JButton("Play");
      audioControlsPane.add(playAudioButton);  
      pauseAudioButton = new JButton("Pause");
      audioControlsPane.add(pauseAudioButton);
      JPanel controlsPane = new JPanel();
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });

      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
      apppcation.loadAudio(AUDIO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Marquee

vlcj - Logo

Let s enhance the apppcation further in which we ll update vlcj - Marquee chapter with Logo support. We can add a text as an overlay over the video.

Create and build a logo


Logo logo = Logo.logo()
   .file(LOGO_PATH)
   .position(LogoPosition.TOP_LEFT)
   .opacity(0.3f)
   .enable();

Set Logo

Now using logo method we can easily set a logo on our video in our apppcation using following syntax−


mediaPlayerComponent.mediaPlayer().logo().set(logo);

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import uk.co.caprica.vlcj.player.base.Logo;
import uk.co.caprica.vlcj.player.base.LogoPosition;
import uk.co.caprica.vlcj.player.base.Marquee;
import uk.co.caprica.vlcj.player.base.MarqueePosition;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3";
   private static final String LOGO_PATH = "D:\Downloads\logo.png";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private final Marquee marquee;
   private final Logo logo;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
	   marquee = Marquee.marquee()
         .text("TutorialsPoint")
         .size(40)
         .colour(Color.WHITE)    		    
         .position(MarqueePosition.BOTTOM_RIGHT)
         .opacity(0.5f)
         .enable();
      mediaPlayerComponent.mediaPlayer().marquee().set(marquee);
	   
      logo = Logo.logo()
         .file(LOGO_PATH)
         .position(LogoPosition.TOP_LEFT)
         .opacity(0.3f)
         .enable();
      mediaPlayerComponent.mediaPlayer().logo().set(logo);
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

      //Create a video border
      Border videoBorder = BorderFactory.createTitledBorder("Video Controls");

      //Create an audio border
      Border audioBorder = BorderFactory.createTitledBorder("Audio Controls");

      JPanel videoControlsPane = new JPanel(); 
      videoControlsPane.setBorder(videoBorder);
      playButton = new JButton("Play");
      videoControlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      videoControlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      videoControlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      videoControlsPane.add(skipButton);
      JPanel audioControlsPane = new JPanel(); 
      audioControlsPane.setBorder(audioBorder);
      playAudioButton = new JButton("Play");
      audioControlsPane.add(playAudioButton);  
      pauseAudioButton = new JButton("Pause");
      audioControlsPane.add(pauseAudioButton);
      JPanel controlsPane = new JPanel();
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      }
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
      apppcation.loadAudio(AUDIO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Logo

vlcj - Media Information

Let s enhance the apppcation further in which we ll update vlcj - Logo chapter with Media Information support.

Parse Media using mediaPlayer


mediaPlayerComponent.mediaPlayer().media().parsing().parse();

Handle Parsing Event


mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
   @Override
   pubpc void mediaParsedChanged(Media media, MediaParsedStatus newStatus) {
   }
}

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.media.MediaEventAdapter;
import uk.co.caprica.vlcj.media.MediaParsedStatus;
import uk.co.caprica.vlcj.media.TrackInfo;
import uk.co.caprica.vlcj.player.base.ChapterDescription;
import uk.co.caprica.vlcj.player.base.Logo;
import uk.co.caprica.vlcj.player.base.LogoPosition;
import uk.co.caprica.vlcj.player.base.Marquee;
import uk.co.caprica.vlcj.player.base.MarqueePosition;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.base.TitleDescription;
import uk.co.caprica.vlcj.player.base.TrackDescription;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3";
   private static final String LOGO_PATH = "D:\Downloads\logo.png";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private final Marquee marquee;
   private final Logo logo;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };		
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
      marquee = Marquee.marquee()
         .text("TutorialsPoint")
         .size(40)
         .colour(Color.WHITE)    		    
         .position(MarqueePosition.BOTTOM_RIGHT)
         .opacity(0.5f)
         .enable();
      mediaPlayerComponent.mediaPlayer().marquee().set(marquee);
	   logo = Logo.logo()
         .file(LOGO_PATH)
         .position(LogoPosition.TOP_LEFT)
         .opacity(0.3f)
         .enable();
      mediaPlayerComponent.mediaPlayer().logo().set(logo);
      mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
         @Override
         pubpc void mediaParsedChanged(Media media,
            MediaParsedStatus newStatus) {
            if(newStatus == MediaParsedStatus.DONE) {
               MediaPlayer mediaPlayer = mediaPlayerComponent.mediaPlayer();
               List<TrackDescription> videoTracks = mediaPlayer.video().trackDescriptions();
               System.out.println(videoTracks.get(1));
               List<TitleDescription> titles = mediaPlayer.titles().titleDescriptions();
               if(!titles.isEmpty()) {
                  System.out.println(titles.get(0));  
               }
               List<ChapterDescription> chapters = mediaPlayer.chapters().descriptions();
               if(!chapters.isEmpty()) {
                  System.out.println(chapters.get(0));  
               }
               List<? extends TrackInfo> trackInfo = mediaPlayer.media().info().tracks();
               if(!trackInfo.isEmpty()) {
                  System.out.println(trackInfo.get(0));  
               }    			  
            }
         }
      });
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

      //Create a video border
      Border videoBorder = BorderFactory.createTitledBorder("Video Controls");

      //Create an audio border
      Border audioBorder = BorderFactory.createTitledBorder("Audio Controls");

      JPanel videoControlsPane = new JPanel(); 
      videoControlsPane.setBorder(videoBorder);
      playButton = new JButton("Play");
      videoControlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      videoControlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      videoControlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      videoControlsPane.add(skipButton);
      JPanel audioControlsPane = new JPanel(); 
      audioControlsPane.setBorder(audioBorder);
      playAudioButton = new JButton("Play");
      audioControlsPane.add(playAudioButton);  
      pauseAudioButton = new JButton("Pause");
      audioControlsPane.add(pauseAudioButton);
      JPanel controlsPane = new JPanel();
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      contentPane.add(controlsPane, BorderLayout.SOUTH);
      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);
      mediaPlayerComponent.mediaPlayer().media().parsing().parse();	  
   }
   pubpc void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
      apppcation.loadAudio(AUDIO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Logo

And it will show messages as follows−


TrackDescription[id=0,description=Track 1]
VideoTrackInfo[codec=0x34363268,codecName=h264,originalCodec=0x31637661,
originalCodecName=avc1,id=0,profile=-1,level=-1,bitRate=0,language=null,
description=null,codecDescription=H264 - MPEG-4 AVC (part 10)]
[width=596,height=336,sampleAspectRatio=1,sampleAspectRatioBase=1,
frameRate=30000,frameRateBase=1001,orientation=TOP_LEFT,projection=RECTANGULAR,
yaw=0.0,pitch=0.0,roll=0.0,fov=80.0,multiview=null]
Media Playback started.
TrackDescription[id=0,description=Track 1]
VideoTrackInfo[codec=0x34363268,codecName=h264,originalCodec=0x31637661,
originalCodecName=avc1,id=0,profile=-1,level=-1,bitRate=0,language=null,
description=null,codecDescription=H264 - MPEG-4 AVC (part 10)]
[width=596,height=336,sampleAspectRatio=1,sampleAspectRatioBase=1,
frameRate=30000,frameRateBase=1001,orientation=TOP_LEFT,projection=RECTANGULAR,
yaw=0.0,pitch=0.0,roll=0.0,fov=80.0,multiview=null]

vlcj - Full Screen Video

Let s enhance the apppcation further in which we ll update vlcj - Media Information chapter with Full Screen playback support.

Attach a full screen strategy

We re using AdaptiveFullScreenStrategy which internally uses XFullScreenStrategy, a native full screen strategy for Linux and Win32FullScreenStrategy, a native full screen strategy for windows.


mediaPlayerComponent.mediaPlayer().fullScreen().strategy(
   new AdaptiveFullScreenStrategy(this));

Toggle Full Screen Mode

Following code toggles the full screen mode.


mediaPlayerComponent.mediaPlayer().fullScreen().toggle();

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.media.MediaEventAdapter;
import uk.co.caprica.vlcj.media.MediaParsedStatus;
import uk.co.caprica.vlcj.media.TrackInfo;
import uk.co.caprica.vlcj.player.base.ChapterDescription;
import uk.co.caprica.vlcj.player.base.Logo;
import uk.co.caprica.vlcj.player.base.LogoPosition;
import uk.co.caprica.vlcj.player.base.Marquee;
import uk.co.caprica.vlcj.player.base.MarqueePosition;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.base.TitleDescription;
import uk.co.caprica.vlcj.player.base.TrackDescription;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.fullscreen.adaptive.AdaptiveFullScreenStrategy;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3";
   private static final String LOGO_PATH = "D:\Downloads\logo.png";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private final Marquee marquee;
   private final Logo logo;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;
   private JToggleButton toggleButton;
   
   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };		
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
      marquee = Marquee.marquee()
         .text("TutorialsPoint")
         .size(40)
         .colour(Color.WHITE)    		    
         .position(MarqueePosition.BOTTOM_RIGHT)
         .opacity(0.5f)
         .enable();
      mediaPlayerComponent.mediaPlayer().marquee().set(marquee);
      logo = Logo.logo()
         .file(LOGO_PATH)
         .position(LogoPosition.TOP_LEFT)
         .opacity(0.3f)
         .enable();
      mediaPlayerComponent.mediaPlayer().logo().set(logo);
      mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
         @Override
         pubpc void mediaParsedChanged(Media media,
            MediaParsedStatus newStatus) {
            if(newStatus == MediaParsedStatus.DONE) {
               MediaPlayer mediaPlayer = mediaPlayerComponent.mediaPlayer();
               List<TrackDescription> videoTracks = mediaPlayer.video().trackDescriptions();
               System.out.println(videoTracks.get(1));

               List<TitleDescription> titles = mediaPlayer.titles().titleDescriptions();
               if(!titles.isEmpty()) {
                  System.out.println(titles.get(0));  
               }
               List<ChapterDescription> chapters = mediaPlayer.chapters().descriptions();
               if(!chapters.isEmpty()) {
                  System.out.println(chapters.get(0));  
               }
               List<? extends TrackInfo> trackInfo = mediaPlayer.media().info().tracks();
               if(!trackInfo.isEmpty()) {
                  System.out.println(trackInfo.get(0));  
               }    			  
            }
         }
      });
      mediaPlayerComponent.mediaPlayer().fullScreen().strategy(new AdaptiveFullScreenStrategy(this));
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

      //Create a video border
      Border videoBorder = BorderFactory.createTitledBorder("Video Controls");

      //Create an audio border
      Border audioBorder = BorderFactory.createTitledBorder("Audio Controls");

      //Create others border
      Border othersBorder = BorderFactory.createTitledBorder("Others");

      JPanel videoControlsPane = new JPanel(); 
      videoControlsPane.setBorder(videoBorder);
      playButton = new JButton("Play");
      videoControlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      videoControlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      videoControlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      videoControlsPane.add(skipButton);
      
      JPanel audioControlsPane = new JPanel(); 
      audioControlsPane.setBorder(audioBorder);
      playAudioButton = new JButton("Play");
      audioControlsPane.add(playAudioButton);  
      pauseAudioButton = new JButton("Pause");
      audioControlsPane.add(pauseAudioButton);
      
      JPanel othersPane = new JPanel(); 
      othersPane.setBorder(othersBorder);
      toggleButton = new JToggleButton("Toggle Full Screen");
      othersPane.add(toggleButton);	  
      JPanel controlsPane = new JPanel();
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      controlsPane.add(othersPane);
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      toggleButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().fullScreen().toggle();
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);
      mediaPlayerComponent.mediaPlayer().media().parsing().parse();	  
   }
   pubpc void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      }
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
      apppcation.loadAudio(AUDIO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Normal Screen

Now cpck on Full Screen Button and see the video in full screen.

Full Screen

vlcj - Audio Equapzer

Let s enhance the apppcation further in which we ll update vlcj - Full Screen Video chapter with an audio equapzer support.

Create an audio equapzer

We can create an equapzer with all values as zeroes using MediaPlayerFactory instance.


Equapzer equapzer = mediaPlayerComponent.mediaPlayerFactory().equapzer().newEquapzer();

Get preset equapzer values

Following code get the preset equapzer names e.g. Flat, Classical, Club, Dance, Headphones etc.


List<String> presetNames = factory.equapzer().presets();

Use the preset equapzer.


Equapzer equapzer = mediaPlayerComponent.mediaPlayerFactory().equapzer().newEquapzer(presetName);

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.media.MediaEventAdapter;
import uk.co.caprica.vlcj.media.MediaParsedStatus;
import uk.co.caprica.vlcj.media.TrackInfo;
import uk.co.caprica.vlcj.player.base.ChapterDescription;
import uk.co.caprica.vlcj.player.base.Equapzer;
import uk.co.caprica.vlcj.player.base.Logo;
import uk.co.caprica.vlcj.player.base.LogoPosition;
import uk.co.caprica.vlcj.player.base.Marquee;
import uk.co.caprica.vlcj.player.base.MarqueePosition;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.base.TitleDescription;
import uk.co.caprica.vlcj.player.base.TrackDescription;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.fullscreen.adaptive.AdaptiveFullScreenStrategy;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3";
   private static final String LOGO_PATH = "D:\Downloads\logo.png";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private final Marquee marquee;
   private final Logo logo;
   private final Equapzer equapzer;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;
   private JToggleButton toggleButton;
   private JButton equapzerButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
      marquee = Marquee.marquee()
         .text("TutorialsPoint")
         .size(40)
         .colour(Color.WHITE)    		    
         .position(MarqueePosition.BOTTOM_RIGHT)
         .opacity(0.5f)
         .enable();
      mediaPlayerComponent.mediaPlayer().marquee().set(marquee);
      logo = Logo.logo()
         .file(LOGO_PATH)
         .position(LogoPosition.TOP_LEFT)
         .opacity(0.3f)
         .enable();
      mediaPlayerComponent.mediaPlayer().logo().set(logo);
      mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
         @Override
         pubpc void mediaParsedChanged(Media media,
            MediaParsedStatus newStatus) {
            if(newStatus == MediaParsedStatus.DONE) {
               MediaPlayer mediaPlayer = mediaPlayerComponent.mediaPlayer();

               List<TrackDescription> videoTracks = mediaPlayer.video().trackDescriptions();
               System.out.println(videoTracks.get(1));
   
               List<TitleDescription> titles = mediaPlayer.titles().titleDescriptions();
               if(!titles.isEmpty()) {
                  System.out.println(titles.get(0));  
               }
               List<ChapterDescription> chapters = mediaPlayer.chapters().descriptions();
               if(!chapters.isEmpty()) {
                  System.out.println(chapters.get(0));  
               }
               List<? extends TrackInfo> trackInfo = mediaPlayer.media().info().tracks();
               if(!trackInfo.isEmpty()) {
                  System.out.println(trackInfo.get(0));  
               }    			  
            }
         }
      });
      mediaPlayerComponent.mediaPlayer().fullScreen().strategy(new AdaptiveFullScreenStrategy(this));

      List<String> presetNames = mediaPlayerComponent.mediaPlayerFactory().equapzer().presets();
      System.out.println(presetNames);
      equapzer =  mediaPlayerComponent.mediaPlayerFactory().equapzer().newEquapzer("Dance");
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

      //Create a video border
      Border videoBorder = BorderFactory.createTitledBorder("Video Controls");

      //Create an audio border
      Border audioBorder = BorderFactory.createTitledBorder("Audio Controls");

      //Create others border
      Border othersBorder = BorderFactory.createTitledBorder("Others");

      JPanel videoControlsPane = new JPanel(); 
      videoControlsPane.setBorder(videoBorder);
      playButton = new JButton("Play");
      videoControlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      videoControlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      videoControlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      videoControlsPane.add(skipButton);

      JPanel audioControlsPane = new JPanel(); 
      audioControlsPane.setBorder(audioBorder);
      playAudioButton = new JButton("Play");
      audioControlsPane.add(playAudioButton);  
      pauseAudioButton = new JButton("Pause");
      audioControlsPane.add(pauseAudioButton);

      JPanel othersPane = new JPanel(); 
      othersPane.setBorder(othersBorder);
      toggleButton = new JToggleButton("Toggle Full Screen");
      equapzerButton = new JButton("Equapzer");
      othersPane.add(toggleButton);
      othersPane.add(equapzerButton);

      JPanel controlsPane = new JPanel();      
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      controlsPane.add(othersPane);
      controlsPane.setPreferredSize(new Dimension(400, 120));
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      toggleButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().fullScreen().toggle();
         }
      });
      equapzerButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().audio().setEquapzer(equapzer);
            System.out.println("Using equapzer: " + equapzer);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);
      mediaPlayerComponent.mediaPlayer().media().parsing().parse();	  
   }
   pubpc void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
      apppcation.loadAudio(AUDIO_PATH);
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Equapzer

Now cpck on Equapzer Button and see the equapzer details in console as follows.


[Flat, Classical, Club, Dance, Full bass, Full bass and treble, 
Full treble, Headphones, Large Hall, Live, Party, Pop, Reggae, 
Rock, Ska, Soft, Soft rock, Techno]
TrackDescription[id=0,description=Track 1]
VideoTrackInfo[codec=0x34363268,codecName=h264,originalCodec=0x31637661,
originalCodecName=avc1,id=0,profile=-1,level=-1,bitRate=0,language=null,
description=null,codecDescription=H264 - MPEG-4 AVC (part 10)]
[width=596,height=336,sampleAspectRatio=1,sampleAspectRatioBase=1,
frameRate=30000,frameRateBase=1001,orientation=TOP_LEFT,
projection=RECTANGULAR,yaw=0.0,pitch=0.0,roll=0.0,fov=80.0,multiview=null]
Media Playback started.
TrackDescription[id=0,description=Track 1]
VideoTrackInfo[codec=0x34363268,codecName=h264,originalCodec=0x31637661,
originalCodecName=avc1,id=0,profile=-1,level=-1,bitRate=0,language=null,
description=null,codecDescription=H264 - MPEG-4 AVC (part 10)]
[width=596,height=336,sampleAspectRatio=1,sampleAspectRatioBase=1,
frameRate=30000,frameRateBase=1001,orientation=TOP_LEFT,projection=RECTANGULAR,
yaw=0.0,pitch=0.0,roll=0.0,fov=80.0,multiview=null]
Using equapzer: Equapzer[preamp=5.0,bandAmps=[9.6, 7.2, 2.4, 
-1.11022E-15, -1.11022E-15, -5.6, -7.2, -7.2, -1.11022E-15, -1.11022E-15]]

vlcj - Overlay

Let s enhance the apppcation further in which we ll update vlcj - Audio Equapzer chapter with an overlay support.

Create an Overlay

We can create an overlay as a swing window to be shown over a video.


class Overlay extends Window {
   pubpc Overlay(Window owner) {
      super(owner, WindowUtils.getAlphaCompatibleGraphicsConfiguration());
      setBackground(new Color(0, 0, 0, 0));
   }
   @Override
   pubpc void paint(Graphics g) {
      super.paint(g);
   }
}

Create and Set Overlay

Following code set the overlay for the media.


Overlay overlay = new Overlay(this); 
mediaPlayerComponent.mediaPlayer().overlay().set(overlay);

Enable/Disable Overlay on demand.


mediaPlayerComponent.mediaPlayer().overlay().enable(overlayEnabled);

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import com.sun.jna.platform.WindowUtils;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.media.MediaEventAdapter;
import uk.co.caprica.vlcj.media.MediaParsedStatus;
import uk.co.caprica.vlcj.media.TrackInfo;
import uk.co.caprica.vlcj.player.base.ChapterDescription;
import uk.co.caprica.vlcj.player.base.Equapzer;
import uk.co.caprica.vlcj.player.base.Logo;
import uk.co.caprica.vlcj.player.base.LogoPosition;
import uk.co.caprica.vlcj.player.base.Marquee;
import uk.co.caprica.vlcj.player.base.MarqueePosition;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.base.TitleDescription;
import uk.co.caprica.vlcj.player.base.TrackDescription;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.fullscreen.adaptive.AdaptiveFullScreenStrategy;

pubpc class App extends JFrame {
   private static final long serialVersionUID = 1L;
   private static final String TITLE = "My First Media Player";
   private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4";
   private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3";
   private static final String LOGO_PATH = "D:\Downloads\logo.png";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private final Marquee marquee;
   private final Logo logo;
   private final Equapzer equapzer;
   private final Overlay overlay;
   private boolean overlayEnabled = false;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;
   private JToggleButton toggleButton;
   private JButton equapzerButton;
   private JToggleButton overlayButton;

   pubpc App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         pubpc void mouseCpcked(MouseEvent e) {
            super.mouseCpcked(e);
            System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         pubpc void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         pubpc void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         pubpc void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            SwingUtipties.invokeLater(new Runnable() {        		
               pubpc void run() {
                  System.out.println("Failed to load Media.");
               }        	       
            });
         }
      };
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         pubpc void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         pubpc void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
      marquee = Marquee.marquee()
         .text("TutorialsPoint")
         .size(40)
         .colour(Color.WHITE)    		    
         .position(MarqueePosition.BOTTOM_RIGHT)
         .opacity(0.5f)
         .enable();
      mediaPlayerComponent.mediaPlayer().marquee().set(marquee);
      logo = Logo.logo()
         .file(LOGO_PATH)
         .position(LogoPosition.TOP_LEFT)
         .opacity(0.3f)
         .enable();
      mediaPlayerComponent.mediaPlayer().logo().set(logo);
      mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
         @Override
         pubpc void mediaParsedChanged(Media media,
            MediaParsedStatus newStatus) {
            if(newStatus == MediaParsedStatus.DONE) {
               MediaPlayer mediaPlayer = mediaPlayerComponent.mediaPlayer();

               List<TrackDescription> videoTracks = mediaPlayer.video().trackDescriptions();
               System.out.println(videoTracks.get(1));
   
               List<TitleDescription> titles = mediaPlayer.titles().titleDescriptions();
               if(!titles.isEmpty()) {
                  System.out.println(titles.get(0));  
               }
               List<ChapterDescription> chapters = mediaPlayer.chapters().descriptions();
               if(!chapters.isEmpty()) {
                  System.out.println(chapters.get(0));  
               }
               List<? extends TrackInfo> trackInfo = mediaPlayer.media().info().tracks();
               if(!trackInfo.isEmpty()) {
                  System.out.println(trackInfo.get(0));  
               }    			  
            }
         }
      });
      mediaPlayerComponent.mediaPlayer().fullScreen().strategy(new AdaptiveFullScreenStrategy(this));
      List<String> presetNames = mediaPlayerComponent.mediaPlayerFactory().equapzer().presets();
      System.out.println(presetNames);
      equapzer =  mediaPlayerComponent.mediaPlayerFactory().equapzer().newEquapzer("Dance");
      overlay = new Overlay(this); 
      mediaPlayerComponent.mediaPlayer().overlay().set(overlay);
   }
   pubpc void initiapze() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         pubpc void windowClosing(WindowEvent e) {
            mediaPlayerComponent.release();
            System.exit(0);
         }
      });    	
      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());   	 
      contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

      //Create a video border
      Border videoBorder = BorderFactory.createTitledBorder("Video Controls");

      //Create an audio border
      Border audioBorder = BorderFactory.createTitledBorder("Audio Controls");

      //Create others border
      Border othersBorder = BorderFactory.createTitledBorder("Others");

      JPanel videoControlsPane = new JPanel(); 
      videoControlsPane.setBorder(videoBorder);
      playButton = new JButton("Play");
      videoControlsPane.add(playButton);    
      pauseButton = new JButton("Pause");
      videoControlsPane.add(pauseButton);
      rewindButton = new JButton("Rewind");
      videoControlsPane.add(rewindButton);
      skipButton = new JButton("Skip");
      videoControlsPane.add(skipButton);

      JPanel audioControlsPane = new JPanel(); 
      audioControlsPane.setBorder(audioBorder);
      playAudioButton = new JButton("Play");
      audioControlsPane.add(playAudioButton);  
      pauseAudioButton = new JButton("Pause");
      audioControlsPane.add(pauseAudioButton);

      JPanel othersPane = new JPanel(); 
      othersPane.setBorder(othersBorder);
      toggleButton = new JToggleButton("Toggle Full Screen");
      equapzerButton = new JButton("Equapzer");
      overlayButton = new JToggleButton("Toggle Overlay");
      othersPane.add(toggleButton);
      othersPane.add(equapzerButton);
      othersPane.add(overlayButton);

      JPanel controlsPane = new JPanel();      
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      controlsPane.add(othersPane);
      controlsPane.setPreferredSize(new Dimension(400, 120));
      contentPane.add(controlsPane, BorderLayout.SOUTH);

      playButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      toggleButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().fullScreen().toggle();
         }
      });
      equapzerButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().audio().setEquapzer(equapzer);
            System.out.println("Using equapzer: " + equapzer);
         }
      });
      overlayButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            overlayEnabled = !overlayEnabled;
            mediaPlayerComponent.mediaPlayer().overlay().enable(overlayEnabled);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);
      mediaPlayerComponent.mediaPlayer().media().parsing().parse();	  
   }
   pubpc void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);   	
   }
   pubpc static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App apppcation = new App(TITLE);
      apppcation.initiapze(); 
      apppcation.setVisible(true);  
      apppcation.loadVideo(VIDEO_PATH);
      apppcation.loadAudio(AUDIO_PATH);
   }
}
class Overlay extends Window {
   pubpc Overlay(Window owner) {
      super(owner, WindowUtils.getAlphaCompatibleGraphicsConfiguration());
      setBackground(new Color(0, 0, 0, 0));
   }
   @Override
   pubpc void paint(Graphics g) {
      super.paint(g);
      Graphics2D g2 = (Graphics2D)g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      GradientPaint gp = new GradientPaint(
         180.0f, 280.0f,
         new Color(255, 255, 255, 255),
         250.0f,
         380.0f,
         new Color(255, 255, 0, 0)
      );
      g2.setPaint(gp);
      g2.setFont(new Font("Serif", Font.PLAIN,32));
      g2.drawString("TutorialsPoint",200, 200); 
   }
}

Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −

Overlay

Now cpck on Toggle Overlay Button and see the overlay text over Video as follows.

Overlay Text Advertisements