2 Replies - 2334 Views - Last Post: 05 November 2009 - 08:05 AM Rate Topic: -----

#1 jasneet06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 09

java media player

Post icon  Posted 30 October 2009 - 12:15 AM

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();
}
}
}
}
this is my cose for media player.. video is played without audio.. plz help...

Is This A Good Question/Topic? 0
  • +

Replies To: java media player

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: java media player

Posted 30 October 2009 - 03:20 PM

:code: (Rule #4 of this forum)

What is the question ?
What is your problem ? In which class/method ?
Was This Post Helpful? 0
  • +
  • -

#3 jasneet06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 09

Re: java media player

Posted 05 November 2009 - 08:05 AM

View Postpbl, on 30 Oct, 2009 - 02:20 PM, said:

:code: (Rule #4 of this forum)

What is the question ?
What is your problem ? In which class/method ?

if i try to play a video in this code.... it is played widout the audio.... plz..
also i want to add a playlist to this code... how do i add it????
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1