Chamar video sem filechooser usando jmf.[Resolvido]

1 resposta
abdneto

Saudações a todos, estou querendo chamar o video sem passar pelo file chooser, estou usando o exemplo do deitel
está funcionando tudo corretamente, só q pra eu implementar na minha aplicação não posso usar filechooser.
segue os códigos:

Classe MediaPanel

package Teste;
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() ); // use a BorderLayout
      
      // Use lightweight components for Swing compatibility
      Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
      
      try
      {
         // create a player to play the media specified in the URL
         Player mediaPlayer = Manager.createRealizedPlayer( mediaURL );
         
         // get the components for the video and the playback controls
         Component video = mediaPlayer.getVisualComponent();
         Component controls = mediaPlayer.getControlPanelComponent();
         
         if ( video != null ) 
            add( video, BorderLayout.CENTER ); // add video component

         if ( controls != null ) 
            add( controls, BorderLayout.SOUTH ); // add controls
         
         mediaPlayer.start(); // start playing the media clip
      } // end try
      catch ( NoPlayerException noPlayerException )
      {
         System.err.println( "No media player found" );
      } // end catch
      catch ( CannotRealizeException cannotRealizeException )
      {
         System.err.println( "Could not realize media player" );
      } // end catch
      catch ( IOException iOException )
      {
         System.err.println( "Error reading from the source" );
      } // end catch
   } // end MediaPanel constructor
} // end class MediaPanel

Classe MediaTest

package Teste;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class MediaTest
{
   // launch the application
   public static void main( String args[] )
   {
      // create a file chooser
      JFileChooser fileChooser = new JFileChooser();
      
      // show open file dialog
      int result = fileChooser.showOpenDialog( null );

      if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
      {
         URL mediaURL = null;


         
         try
         {
            // get the file as URL
            mediaURL = fileChooser.getSelectedFile().toURL();
         } // end try
         catch ( MalformedURLException malformedURLException )
         {
            System.err.println( "Could not create URL for the file" );
         } // end catch

         if ( mediaURL != null ) // only display if there is a valid URL
         {
            JFrame mediaTest = new JFrame( "Mangue Media Player" );
            mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            
            MediaPanel mediaPanel = new MediaPanel( mediaURL );
            mediaTest.add( mediaPanel );
            
            mediaTest.setSize( 300, 300 );
            mediaTest.setLocation(500, 100);
            mediaTest.setVisible( true );
         } // end inner if
      } // end outer if
   } // end main

} // end class MediaTest

Preciso chamar a classe MediaTest atráves de um botão dentro da minha aplicação.
Desde já obrigado, no aguardo de ajuda e informações.

1 Resposta

abdneto

Problema resolvido. tive que alterar a classe MediaTest.
Segue abaixo o código alterado:

Classe MediaTest

package Teste;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class MediaTest
{
   // inicia a aplicação
   public static void main( String args[] )throws MalformedURLException
   {
         String video = "/C:/caminho_do_arquivo/nome_do_arquivo.mpeg";
         File arquivo_video = new File(video);

         URL mediaURL = null;

         try{
               mediaURL = arquivo_video.toURL();
         }catch(MalformedURLException malformedURLException){
               System.err.println("Não foi possível criar um URL do arquivo");
         }
         if(mediaURL != null){
              MediaPanel mediaPanel = new MediaPanel(mediaURL);
         }

         if ( mediaURL != null ) // exibi somente se houver uma URL válida
         {
            JFrame mediaTest = new JFrame( "Mangue Media Player" );
            mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            
            MediaPanel mediaPanel = new MediaPanel( mediaURL );
            mediaTest.add( mediaPanel );
            //System.out.println("URL: "+mediaURL);
            mediaTest.setSize( 300, 300 );
            mediaTest.setLocation(500, 100);
            mediaTest.setVisible( true );
         } 
      } 
}

Obrigado a todos.

Criado 29 de novembro de 2010
Ultima resposta 30 de nov. de 2010
Respostas 1
Participantes 1