java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4084
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Make your own Java Media Player to play media files E-mail
User Rating: / 601
PoorBest 

This Java Swing tip illustrates a method of programming your own Java Media Player to play media files. A Java Media Framework player fundamentally is an encapsulation of the multimedia component that allows for control of state transitions during playback. JMF players provide methods to query the current state, to acquire necessary resources, and to start, stop, and control the actual playback of the media file or stream.

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 actionPerformedActionEvent e )
            {
               openFile();
               createPlayer();
            }
         }
      );
      getContentPane().addopenFile, BorderLayout.NORTH );
  
      setSize300300 );
      show();
   }

   private void openFile()
   {      
      JFileChooser fileChooser = new JFileChooser();

      fileChooser.setFileSelectionMode(
         JFileChooser.FILES_ONLY );
      int result = fileChooser.showOpenDialogthis );

      // 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.createPlayerfile.toURL() );
         player.addControllerListenernew EventHandler() );
         player.start();  // start player
      }
      catch Exception e ){
         JOptionPane.showMessageDialogthis,
            "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.removevisual );

      if control != null 
         c.removecontrol );
   }

   public static void main(String args[])
   {
      MediaPlayerDemo app = new MediaPlayerDemo();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosingWindowEvent e )
            {
               System.exit(0);
            }
         }
      );
   }

   // inner class to handler events from media player
   private class EventHandler implements ControllerListener {
      public void controllerUpdateControllerEvent e ) {
         if instanceof RealizeCompleteEvent ) {
            Container c = getContentPane();
         
            // load Visual and Control components if they exist
            Component visualComponent =
               player.getVisualComponent();

            if visualComponent != null )
               c.addvisualComponent, BorderLayout.CENTER );

            Component controlsComponent =
               player.getControlPanelComponent();

            if controlsComponent != null )
               c.addcontrolsComponent, BorderLayout.SOUTH );

            c.doLayout();
         }
      }
   }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.