Author Topic: If any1 have  (Read 997 times)

Offline Sai.InkBeast

  • Hero Member
  • *
  • Topic Author
  • Posts: 707
  • Karma: +33/-49
  • That could be a real trip man
    • View Profile
    • Awards
If any1 have
« on: April 15, 2011, 14:20 »
Need syntax for Java to make Media player. Thanks ^^


Eurobattle.net account: Lone_Druid

Dota League

Offline BeansPowered

  • Advanced Member
  • *
  • Posts: 182
  • Karma: +32/-54
  • Be free to join another community! HI Dotalicious!
    • View Profile
    • Awards
Re: If any1 have
« Reply #1 on: April 15, 2011, 15:33 »
Your question sux even more than your topic's name.

What the heck do you mean by "java syntax"? You want to create your own media player using Java?

You wants tips to start from scratch? Or the full code source?

Offline mammed

  • Advanced Member
  • *
  • Posts: 175
  • Karma: +15/-15
    • View Profile
    • Awards
Re: If any1 have
« Reply #2 on: April 15, 2011, 16:12 »
That will play media from a URL, hf.

Spoiler for Hiden:
Code: [Select]
1     // Fig. 21.6: MediaPanel.java
 2     // A JPanel the plays media from a URL
 3     import java.awt.BorderLayout;
 4     import java.awt.Component;
 5     import java.io.IOException;
 6     import java.net.URL;
 7     import javax.media.CannotRealizeException;
 8     import javax.media.Manager;
 9     import javax.media.NoPlayerException;
 10    import javax.media.Player;
 11    import javax.swing.JPanel;
 12   
 13    public class MediaPanel extends JPanel
 14    {
 15       public MediaPanel( URL mediaURL )
 16       {
 17          setLayout( new BorderLayout() ); // use a BorderLayout
 18   
 19           // Use lightweight components for Swing compatibility
 20    Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
 21         
 22          try
 23          {
 24             // create a player to play the media specified in the URL
 25             Player mediaPlayer = Manager.createRealizedPlayer( mediaURL );
 26   
 27             // get the components for the video and the playback controls
 28             Component video = mediaPlayer.getVisualComponent();
 29             Component controls = mediaPlayer.getControlPanelComponent();
 30           
 31             if ( video != null )
 32                add( video, BorderLayout.CENTER ); // add video component
 33   
 34             if ( controls != null )
 35                add( controls, BorderLayout.SOUTH ); // add controls
 36   
 37             mediaPlayer.start(); // start playing the media clip
 38          } // end try
 39          catch ( NoPlayerException noPlayerException )
 40          {
 41             System.err.println( "No media player found" );
 42          } // end catch
 43          catch ( CannotRealizeException cannotRealizeException )
 44    {
 45             System.err.println( "Could not realize media player" );
 46          } // end catch
 47          catch ( IOException iOException )
 48    {
 49             System.err.println( "Error reading from the source" );
 50          } // end catch
 51       } // end MediaPanel constructor
 52    } // end class MediaPanel

Here's 2nd one

Spoiler for Hiden:
Code: [Select]
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();
         }
      }
   }
}
« Last Edit: April 15, 2011, 16:23 by mammed »

Offline Sai.InkBeast

  • Hero Member
  • *
  • Topic Author
  • Posts: 707
  • Karma: +33/-49
  • That could be a real trip man
    • View Profile
    • Awards
Re: If any1 have
« Reply #3 on: April 15, 2011, 21:11 »
Tnx mammed. I programed it x)


Eurobattle.net account: Lone_Druid

Dota League

Offline mammed

  • Advanced Member
  • *
  • Posts: 175
  • Karma: +15/-15
    • View Profile
    • Awards
Re: If any1 have
« Reply #4 on: April 16, 2011, 08:02 »
you're welcome ;)