The VideoPlayer program allows you select a sound or video file to play. In this program, you learn to use the JMF API to create a player that can be used to play sound (.wav) or video file (.mov, and .mpg) and add visual and control components to the player so user can see the movie and control the movie play . You can download JMF from http://www.sun.com website.
VideoPlayer source code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.media.*;
import javax.media.control.TrackControl;
import javax.media.datasink.*;
import javax.media.format.VideoFormat;
import javax.media.protocol.*;
import java.net.*;
import javax.swing.JFileChooser;
class PlayInterface extends JFrame implements ActionListener{
private Player vPlayer= null;
private Dimension ds=null;
private Dimension sds=null;
private URL url=null;
private JPanel panel;
private JMenuBar mainmenu;
private JMenu menu;
private JMenuItem mopen;
private JMenuItem mexit;
PlayInterface() {
addMenu();
addPanel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
setTitle("Video Player");
setVisible(true);
}
//create menu bar, menu, sub menus and add the menu bar to the program interface
public void addMenu(){
mainmenu=new JMenuBar();
menu=new JMenu("File"); //create menu
menu.setMnemonic(KeyEvent.VK_F);
mopen=new JMenuItem("Open...");
mopen.setMnemonic(KeyEvent.VK_O);
mopen.addActionListener(this);
mexit=new JMenuItem("Exit");
mexit.setMnemonic(KeyEvent.VK_X);
mexit.addActionListener(this);
menu.add(mopen);
menu.add(mexit);
mainmenu.add(menu);
setJMenuBar(mainmenu);
}
//add Panel to the Frame window
public void addPanel(){
panel=new JPanel();
panel.setLayout(null);
panel.setBackground(Color.BLACK);
add(panel, BorderLayout.CENTER);
}
//choose video file, create player, adjust visual and control component, and play the media file
public void prepareVideo(){
sds=getToolkit().getScreenSize(); //get screen dimendion width and height
//initialize height and width variables
int height=0;
int width=0;
try{
url=getFile(); //choose the video file
if(url!=null)
vPlayer=Manager.createRealizedPlayer(url); //create player object
if(vPlayer==null) System.exit(-1); //exit the program if the play can not be created from the url
ds=getVideoDimension(url); //get video dimension--width and height
Component vc=vPlayer.getVisualComponent(); //get visual component to show the video
Component cpc=vPlayer.getControlPanelComponent();//get control component to
//control the playback: play, stop, mute,...
//adjust the width and height of the components based on the widths and heights of
//the screen and video frame
if(ds!=null) //video file is selected so its dimension can be fetched
{
if(ds.getWidth()>=(int)sds.getWidth()-100)
width=(int)sds.getWidth()-100;
else
width=(int)ds.getWidth();
if(ds.getHeight()>=(int)sds.getHeight()-150)
height=(int)sds.getHeight()-150;
else
height=(int)ds.getHeight();
}
else //audio file is selected so the components must be 300px wide and 20px high
{
width=300;
height=20;
}
//specify the size and location of visual component and
//add the visual component to the panel to it is ready to show on the program interface
//if it is not null
if(vc!=null){
vc.setSize(new Dimension(width,height));
vc.setLocation((int)ds.getWidth()/2-width/2,10);
panel.add(vc);
}
//specify the size and location of control component and
//add the control component to the panel to it is ready to show on the program interface
//if it is not null
if(cpc!=null){
cpc.setSize(new Dimension(width,30));
cpc.setLocation((int)ds.getWidth()/2-width/2,height+20);
panel.add(cpc);
}
vPlayer.start(); //start the player
}
catch (Exception e){e.printStackTrace();}
}
public void actionPerformed(ActionEvent e){
JMenuItem source = (JMenuItem)(e.getSource());
if(source.getText().compareTo("Open...")==0) //The Open...sub-menu is selected
{
panel.removeAll(); //clear all components on the Panel
prepareVideo();//select file, create player, add components, play the media
repaint();//repaint the program interface
validate(); //make sure the components display properly
}
else if(source.getText().compareTo("Exit")==0) //The Exit sub-menu is selected
System.exit(0);
}
public URL getFile() throws MalformedURLException{
JFileChooser fileChooser = new JFileChooser(); //create file chooser object
URL url= null;
int result = fileChooser.showOpenDialog( null ); //open the file dialog
if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
url= fileChooser.getSelectedFile().toURI().toURL(); //convert the chosen file path to url
return url;
}
public Dimension getVideoDimension(URL url){
Processor processor = null;
Dimension vds=null;
long startTime;
int timeOut=1000;
try {
DataSource ds= Manager.createDataSource(new MediaLocator(url)); //create datasource from the url
ds.connect(); //connect to the datasource
processor = Manager.createProcessor(ds); //create processor
processor.configure(); //configure the processor
//wait for end of processor configuration
startTime = System.currentTimeMillis();
while(processor.getState()<processor.Configured){
Thread.sleep(timeOut);
if(System.currentTimeMillis()-startTime>timeOut)
break;
}
processor.realize();//realize the processor
//wait for end of processor realization
startTime = System.currentTimeMillis();
while(processor.getState()<processor.Realized){
Thread.sleep(timeOut);
if(System.currentTimeMillis()-startTime>timeOut)
break;
}
TrackControl[] tcs=processor.getTrackControls(); //get the controls of the video track
for(TrackControl tc:tcs){
VideoFormat fv=(VideoFormat)tc.getFormat(); //get the track format from the track control
vds=fv.getSize(); //get the size of the video frame from the format
}
} catch (Exception ex){}
return vds;
}
}
class VideoPlayer{
public static void main(String[] args){
PlayInterface myplayer=new PlayInterface();
}
}
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.media.*;
import javax.media.control.TrackControl;
import javax.media.datasink.*;
import javax.media.format.VideoFormat;
import javax.media.protocol.*;
import java.net.*;
import javax.swing.JFileChooser;
class PlayInterface extends JFrame implements ActionListener{
private Player vPlayer= null;
private Dimension ds=null;
private Dimension sds=null;
private URL url=null;
private JPanel panel;
private JMenuBar mainmenu;
private JMenu menu;
private JMenuItem mopen;
private JMenuItem mexit;
PlayInterface() {
addMenu();
addPanel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
setTitle("Video Player");
setVisible(true);
}
//create menu bar, menu, sub menus and add the menu bar to the program interface
public void addMenu(){
mainmenu=new JMenuBar();
menu=new JMenu("File"); //create menu
menu.setMnemonic(KeyEvent.VK_F);
mopen=new JMenuItem("Open...");
mopen.setMnemonic(KeyEvent.VK_O);
mopen.addActionListener(this);
mexit=new JMenuItem("Exit");
mexit.setMnemonic(KeyEvent.VK_X);
mexit.addActionListener(this);
menu.add(mopen);
menu.add(mexit);
mainmenu.add(menu);
setJMenuBar(mainmenu);
}
//add Panel to the Frame window
public void addPanel(){
panel=new JPanel();
panel.setLayout(null);
panel.setBackground(Color.BLACK);
add(panel, BorderLayout.CENTER);
}
//choose video file, create player, adjust visual and control component, and play the media file
public void prepareVideo(){
sds=getToolkit().getScreenSize(); //get screen dimendion width and height
//initialize height and width variables
int height=0;
int width=0;
try{
url=getFile(); //choose the video file
if(url!=null)
vPlayer=Manager.createRealizedPlayer(url); //create player object
if(vPlayer==null) System.exit(-1); //exit the program if the play can not be created from the url
ds=getVideoDimension(url); //get video dimension--width and height
Component vc=vPlayer.getVisualComponent(); //get visual component to show the video
Component cpc=vPlayer.getControlPanelComponent();//get control component to
//control the playback: play, stop, mute,...
//adjust the width and height of the components based on the widths and heights of
//the screen and video frame
if(ds!=null) //video file is selected so its dimension can be fetched
{
if(ds.getWidth()>=(int)sds.getWidth()-100)
width=(int)sds.getWidth()-100;
else
width=(int)ds.getWidth();
if(ds.getHeight()>=(int)sds.getHeight()-150)
height=(int)sds.getHeight()-150;
else
height=(int)ds.getHeight();
}
else //audio file is selected so the components must be 300px wide and 20px high
{
width=300;
height=20;
}
//specify the size and location of visual component and
//add the visual component to the panel to it is ready to show on the program interface
//if it is not null
if(vc!=null){
vc.setSize(new Dimension(width,height));
vc.setLocation((int)ds.getWidth()/2-width/2,10);
panel.add(vc);
}
//specify the size and location of control component and
//add the control component to the panel to it is ready to show on the program interface
//if it is not null
if(cpc!=null){
cpc.setSize(new Dimension(width,30));
cpc.setLocation((int)ds.getWidth()/2-width/2,height+20);
panel.add(cpc);
}
vPlayer.start(); //start the player
}
catch (Exception e){e.printStackTrace();}
}
public void actionPerformed(ActionEvent e){
JMenuItem source = (JMenuItem)(e.getSource());
if(source.getText().compareTo("Open...")==0) //The Open...sub-menu is selected
{
panel.removeAll(); //clear all components on the Panel
prepareVideo();//select file, create player, add components, play the media
repaint();//repaint the program interface
validate(); //make sure the components display properly
}
else if(source.getText().compareTo("Exit")==0) //The Exit sub-menu is selected
System.exit(0);
}
public URL getFile() throws MalformedURLException{
JFileChooser fileChooser = new JFileChooser(); //create file chooser object
URL url= null;
int result = fileChooser.showOpenDialog( null ); //open the file dialog
if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
url= fileChooser.getSelectedFile().toURI().toURL(); //convert the chosen file path to url
return url;
}
public Dimension getVideoDimension(URL url){
Processor processor = null;
Dimension vds=null;
long startTime;
int timeOut=1000;
try {
DataSource ds= Manager.createDataSource(new MediaLocator(url)); //create datasource from the url
ds.connect(); //connect to the datasource
processor = Manager.createProcessor(ds); //create processor
processor.configure(); //configure the processor
//wait for end of processor configuration
startTime = System.currentTimeMillis();
while(processor.getState()<processor.Configured){
Thread.sleep(timeOut);
if(System.currentTimeMillis()-startTime>timeOut)
break;
}
processor.realize();//realize the processor
//wait for end of processor realization
startTime = System.currentTimeMillis();
while(processor.getState()<processor.Realized){
Thread.sleep(timeOut);
if(System.currentTimeMillis()-startTime>timeOut)
break;
}
TrackControl[] tcs=processor.getTrackControls(); //get the controls of the video track
for(TrackControl tc:tcs){
VideoFormat fv=(VideoFormat)tc.getFormat(); //get the track format from the track control
vds=fv.getSize(); //get the size of the video frame from the format
}
} catch (Exception ex){}
return vds;
}
}
class VideoPlayer{
public static void main(String[] args){
PlayInterface myplayer=new PlayInterface();
}
}
In the program code above, the JFileChooser component is used to allow the user to selection a video file to open. When the path of the video file is obtained, it is time to set up some components to play and controls the video play. A video Player object is created by using the createRealizedPlayer method of the Manager class. Then the getVideoDimension is called to get the width and height of the input view frame. To get the dimension of the video, you need to create a processor that encapsulates the data source of the video file. The processor must be configured and realized properly before you can use it to access information about the video format. The getTrackControls method of the processor object returns all track controls objects found in the video. A track control object contains the video format object. This object contains information about the dimension of the video frame. You can get the dimension by using the getSize method of the video format object. When you have the size or dimension of the video, you can adjust the size of the visual component dynamically. The components to be added to the program are visual component and control panel component. The visual component shows the video while the control panel component controls the video play (play, stop, and change volume status, etc). When these components are successfully set up. You can call the start method of the play to start playing the video file.
-->