developing media player
hi,
I am a newbie to java. I have a basic knowledge of Java and I am currently involved in developing a media player in java. One of my friend who loves open - source (And even converted me to oss world) said about the power of community. i was really amazed. i started working in linux and i was very much fed up with having control over different media players in different desktop environment(KDE, GNOME). This is the reason behind starting this project.,
since i am in a college., I have to develop it in windows and then try it in my home with linux.,
what I have did till now is the layout design of the media player,. that too not completed fully, but a part of it., And can anyone help me in developing it.,
If anyone wish to view what I have did till now, just mail ur e-mail address to me., My e-mail address is karthikvirus@gmail.com.
Waiting for reply,
subramaniam
[919 byte] By [
Spaniana] at [2007-11-26 18:33:59]

# 1
Well, you have to use java swing for your GUI. Check these links:
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html
http://java.sun.com/products/java-media/jmf/2.1.1/guide/JMFTOC.html
http://www.informit.com/articles/article.asp?p=21113&seqNum=4&rl=1
http://www-128.ibm.com/developerworks/java/edu/j-dw-javajmf-i.html?S_TACT=104AHW02
or u can try these:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;
public class MediaPlayerDemo extends JFrame {
private Player player;
private File file;
public MediaPlayerDemo()
{
super( "Demonstrating the Java Media Player" );
JButton openFile = new JButton( "Open file to play" );
openFile.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
openFile();
createPlayer();
}
}
);
getContentPane().add( openFile, BorderLayout.NORTH );
setSize( 300, 300 );
show();
}
private void openFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
// user clicked Cancel button on dialog
if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = fileChooser.getSelectedFile();
}
private void createPlayer()
{
if ( file == null )
return;
removePreviousPlayer();
try {
// create a new player and add listener
player = Manager.createPlayer( file.toURL() );
player.addControllerListener( new EventHandler() );
player.start(); // start player
}
catch ( Exception e ){
JOptionPane.showMessageDialog( this,
"Invalid file or location", "Error loading file",
JOptionPane.ERROR_MESSAGE );
}
}
private void removePreviousPlayer()
{
if ( player == null )
return;
player.close();
Component visual = player.getVisualComponent();
Component control = player.getControlPanelComponent();
Container c = getContentPane();
if ( visual != null )
c.remove( visual );
if ( control != null )
c.remove( control );
}
public static void main(String args[])
{
MediaPlayerDemo app = new MediaPlayerDemo();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
// inner class to handler events from media player
private class EventHandler implements ControllerListener {
public void controllerUpdate( ControllerEvent e ) {
if ( e instanceof RealizeCompleteEvent ) {
Container c = getContentPane();
// load Visual and Control components if they exist
Component visualComponent =
player.getVisualComponent();
if ( visualComponent != null )
c.add( visualComponent, BorderLayout.CENTER );
Component controlsComponent =
player.getControlPanelComponent();
if ( controlsComponent != null )
c.add( controlsComponent, BorderLayout.SOUTH );
c.doLayout();
}
}
}
}
# 5
Hi muskan
This is Srinivas, i got the code for play the .avi files and modified as per my requirement.
If any body wants the code........
// MediaPlayer.java
//package test;
import javax.media.*;
import java.awt.*;
import java.awt.event.*;
class MediaPlayer extends Frame implements ActionListener,
ControllerListener,
ItemListener
{
Player player;
Component vc, cc;
boolean first = true, loop = false;
String currentDirectory;
MediaPlayer (String title)
{
super (title);
addWindowListener
(new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
// User selected close from System menu.
// Call dispose to invoke windowClosed.
dispose ();
}
public void windowClosed (WindowEvent e)
{
if (player != null)
player.close ();
System.exit (0);
}
});
Menu m = new Menu ("File");
MenuItem mi = new MenuItem ("Open...");
mi.addActionListener (this);
m.add (mi);
m.addSeparator ();
CheckboxMenuItem cbmi = new CheckboxMenuItem ("Loop", false);
cbmi.addItemListener (this);
m.add (cbmi);
m.addSeparator ();
mi = new MenuItem ("Exit");
mi.addActionListener (this);
m.add (mi);
MenuBar mb = new MenuBar ();
mb.add (m);
setMenuBar (mb);
setSize (1024, 100);
setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Exit"))
{
// Call dispose to invoke windowClosed.
dispose ();
return;
}
if (player != null)
player.close ();
try
{
currentDirectory = System.getProperty("user.dir");
player = Manager.createPlayer (new MediaLocator
("file:" +
currentDirectory + "\\images\\topimages.avi"));
/*fd.getDirectory () +
fd.getFile ())); */
}
catch (java.io.IOException e2)
{
System.out.println (e2);
return;
}
catch (NoPlayerException e2)
{
System.out.println(currentDirectory);
System.out.println(player);
System.out.println ("Could not find a player.");
return;
}
if (player == null)
{
System.out.println ("Trouble creating a player.");
return;
}
first = false;
setTitle ("TopImages.avi");
player.addControllerListener (this);
player.prefetch ();
}
public void controllerUpdate (ControllerEvent e)
{
// A ControllerClosedEvent is posted when player.close is
// called. If there is a visual component, this component must
// be removed. Otherwise, this visual component appears
// blanked out on the screen. (To be consistent, we do the same
// thing for the control panel component.)
//
// Note: This problem occurs when run under JMF 2.1 Windows and
//SDK 1.3 on a Windows 98 SE platform.
if (e instanceof ControllerClosedEvent)
{
if (vc != null)
{
remove (vc);
vc = null;
}
if (cc != null)
{
remove (cc);
cc = null;
}
return;
}
if (e instanceof EndOfMediaEvent)
{
if (loop)
{
player.setMediaTime (new Time (0));
player.start ();
}
return;
}
if (e instanceof PrefetchCompleteEvent)
{
player.start ();
return;
}
if (e instanceof RealizeCompleteEvent)
{
vc = player.getVisualComponent ();
if (vc != null)
add (vc);
cc = player.getControlPanelComponent ();
if (cc != null)
add (cc, BorderLayout.SOUTH);
pack ();
}
}
public void itemStateChanged (ItemEvent e)
{
loop = !loop;
}
public void paint (Graphics g)
{
if (first)
{
int w = getSize ().width;
int h = getSize ().height;
g.setColor (Color.blue);
g.fillRect (0, 0, w, h);
Font f = new Font ("DialogInput", Font.BOLD, 16);
g.setFont (f);
FontMetrics fm = g.getFontMetrics ();
int swidth = fm.stringWidth ("*** Welcome ***");
g.setColor (Color.white);
g.drawString ("*** Welcome ***",
(w - swidth) / 2,
(h + getInsets ().top) / 2);
}
// Call overridden Frame superclass paint method. That method
// will call each contained container and component (including
// the control panel component) paint method.
super.paint (g);
}
// Eliminate control panel component flicker by preventing frame
// background from being cleared.
public void update (Graphics g)
{
paint (g);
}
public static void main (String [] args)
{
new MediaPlayer ("Media Player 1.0");
}
public void dummy()
{
FileDialog fd = new FileDialog (this, "Open File",
FileDialog.LOAD);
fd.setDirectory (currentDirectory);
fd.show ();
// If user cancelled, exit.
if (fd.getFile () == null)
return;
currentDirectory = fd.getDirectory ();
if (player != null)
player.close ();
try
{
player = Manager.createPlayer (new MediaLocator
("file:" +
currentDirectory + "\\topimages.avi"));
/*fd.getDirectory () +
fd.getFile ())); */
}
catch (java.io.IOException e2)
{
System.out.println (e2);
return;
}
catch (NoPlayerException e2)
{
System.out.println ("Could not find a player.");
return;
}
if (player == null)
{
System.out.println ("Trouble creating a player.");
return;
}
first = false;
setTitle (fd.getFile ());
player.addControllerListener (this);
player.prefetch ();
}
}
Srinivsa Retneni