// Fig. 21.6: MediaPanel.java
// Um JPanel reproduz mídia de um URL
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JPanel;

public class MediaPanel extends JPanel
{
   public MediaPanel( URL mediaURL )
   {
      setLayout( new BorderLayout() ); // utiliza um BorderLayout
      
      // Utiliza componentes leves para compatibilidade Swing
      Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
      
      try
      {
         // cria um player para reproduzir a mídia especificada no URL     
         Player mediaPlayer = Manager.createRealizedPlayer( mediaURL );

         // obtém os componentes para o vídeo e os controles de reprodução
         Component video = mediaPlayer.getVisualComponent();          
         Component controls = mediaPlayer.getControlPanelComponent();
         
         if ( video != null ) 
            add( video, BorderLayout.CENTER ); // adiciona o componente de vídeo

         if ( controls != null ) 
            add( controls, BorderLayout.SOUTH ); // adiciona os controles

         mediaPlayer.start(); // inicia a reprodução do clipe de mídia
      } // fim do try
      catch ( NoPlayerException noPlayerException )
      {
         System.err.println( "No media player found" );
      } // fim do catch
      catch ( CannotRealizeException cannotRealizeException )
      {
         System.err.println( "Could not realize media player" );
      } // fim do catch
      catch ( IOException iOException )
      {
         System.err.println( "Error reading from the source" );
      } // fim do catch
   } // fim do construtor MediaPanel
} // fim da classe MediaPanel 


/**************************************************************************
 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/