DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Java - Example Very Simple Player (JMF)

10.27.2006
| 94836 views |
  • submit to reddit
        // Main Class

package org.jmf.example;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class ExampleJMF
{
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true); 
		JDialog.setDefaultLookAndFeelDecorated(true);
		
		try
		{
			UIManager.setLookAndFeel(new MetalLookAndFeel());
		}
		catch(UnsupportedLookAndFeelException e)
		{
			e.printStackTrace();
		}
		
		new exampleFrame();
	}
}

// Frame Class

package org.jmf.example;

import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class exampleFrame extends JFrame
{
	private static final long serialVersionUID = 1L;
	
	public exampleFrame()
	{
		super("JMF - Example...");
		
		setSize(400, 300);
		setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight())/2);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				System.exit(0);
			}
		});
		
		setContentPane(new examplePanel());
		setVisible(true);
	}
}

// Panel Class

package org.jmf.example;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.JPanel;

public class examplePanel extends JPanel implements ActionListener, ControllerListener
{
	private static final long serialVersionUID = 1L;
	
	private Component visualComponent;
	private Player player;
	
	public examplePanel()
	{
		try
		{
			player = Manager.createPlayer(new URL("file:///tmp/a.mpg"));
			player.addControllerListener(this);
			
			player.start();
		}
		catch(NoPlayerException e)
		{
			e.printStackTrace();
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
	}

	public void actionPerformed(ActionEvent e)
	{

	}

	public void controllerUpdate(ControllerEvent c)
	{
		if(player == null)
			return;
		
		if(c instanceof RealizeCompleteEvent)
		{
			if((visualComponent = player.getVisualComponent()) != null)
				add(visualComponent);
		}
	}
}
    

Comments

Justu Stan replied on Wed, 2014/05/14 - 5:11am

Thanks for sharing the code of the JMF player. I think for the java developers this would be a great thing and no one else has shared the code. I have shared this example of simple player with most of my java-learning friends search engine optimization 

Ibrahim Mahammad replied on Fri, 2012/08/03 - 5:55am

I am using Eclips and trying to run the code but getting the following error.

 

Unable to handle format: MPEG, 400x400, FrameRate=30.0, Length=240000

Failed to realize: com.sun.media.PlaybackEngine@1caefb0

Error: Unable to realize com.sun.media.PlaybackEngine@1caefb0

 Here i am using .mpg format file only.

 Window is opening but file is not playing, only empty window opening.

 

Pls help on this as early as possible. 

Carla Brian replied on Fri, 2012/06/29 - 8:23pm

I am new to this. I am glad to see your post. This could really be helpful. - Mercy Ministries

Snippets Manager replied on Sat, 2011/01/15 - 6:53am

When i try to compile the three files it gives an error on the following: examplePanel.class is created successfully! In exampleFrame.java , setContentPane(new examplePanel()); - an error comes up saying cannot find symbol ( new exampleFrame() ) similarly same error comes up in ExampleJMF.java. How to solve this issue?

Snippets Manager replied on Mon, 2009/05/18 - 2:34am

after compiling & executing this code, nothing seems to be happening...the program just does not do anything!

veeren sw replied on Mon, 2009/03/23 - 11:49am

URL("file:///tmp/a.mpg")); first create a tmp folder in c: drive and put a mpg file in that folder and rename it as a.mpg URL("file:///tmp/a.mpg")); change this line to URL("file:///c:/tmp/a.mpg"));

Snippets Manager replied on Fri, 2009/01/16 - 10:26am

What is the meaning of this URL.... URL("file:///tmp/a.mpg")); How can I customize with my directory.... Plz help me.... Its urgent...

Snippets Manager replied on Fri, 2008/05/16 - 10:05am

I'm able to compile this code ... thanks! But now, I can't seem to locate a sample .mpg file which can actually be successfully read. I get "Unable to handle format:" messages, no matter what mpg file I throw at it. Can someone provide me with pointers to some valid mpg files which can be read by the jmf package? Also, what is the size of the most up-to-date jmf.jar file? thanks in advance.

Snippets Manager replied on Mon, 2008/02/25 - 7:25pm

i try to compile this source code but eclipse throws errors in: player = Manager.createPlayer(new URL("file:///tmp/a.mpg"));//what I should over-write in that command next problem was in: declaration - public examplePanel() {**************** and in setContentPane(new examplePanel());