Jmf nao consigo fechar automatico o video quando terminado a execução

0 respostas
R

ola pessoal,
Gostaria muito que alguem me desse uma ajuda neste pequeno problema, andei vendo alguns exemplos e fazendo algumas alteração, preciso que apos inicializado o video quando ele terminar ele feche e volte para a tela de escolha de um novo video segue o codigo das duas classe que estou usando:

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Rectangle;

import javax.media.CachingControl;
import javax.media.CachingControlEvent;
import javax.media.ControllerErrorEvent;
import javax.media.ControllerEvent;
import javax.media.EndOfMediaEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.media.bean.playerbean.MediaPlayer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.sun.media.ui.ProgressBar;
import java.io.*;
import com.sun.media.protocol.file.DataSource;

public class ExecutaVideo extends JFrame{

    private static final Rectangle Rectangle = null;
	Container cp;
    MediaPlayer player;
    String vd = null;
    

    public ExecutaVideo() {

    

        setTitle("Tocador de Video");
        setBounds(new Rectangle(1, 1, 1280, 1024));
        setResizable(false);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        centerJFrame(this);
       

    }

    private void centerJFrame (JFrame frame) {

        Dimension paneSize      = frame.getSize();
        Dimension screenSize    = frame.getToolkit().getScreenSize();
        frame.setLocation( (screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);

    }
    public void start() 
      {
    	String fille = "file:/"+ vd +".mpeg";
    	
    	try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {}

        cp      = getContentPane();

        player  = new MediaPlayer();
        player.setPlaybackLoop(false);
       
		
        //Abre um arquivo no diretorio raiz com o nome ""
        player.setMediaLocation(fille);

        cp.add(player);
      
       // Call start() to prefetch and start the player.
       
       if (player != null) player.start();
      
     // Time d = player.getDuration().TIME_UNKNOWN;
 
   
       
       
       
    }

    /**
     * Stop media file playback and release resources before
     * leaving the page.
     */
    
     
    public void stop() 
    {
       if (player != null)
      
          player.stop();
          player.close();
          player.deallocate();
       
    }
    
    /**
     * This controllerUpdate function must be defined in order
     * to implement a ControllerListener interface.  This
     * function will be called whenever there is a media event.
     */
     
    public synchronized void controllerUpdate(ControllerEvent event) 
    {
       // If we're getting messages from a dead player, 
       // just leave
       
       if (player == null) return;

       // When the player is Realized, get the visual
       // and control components and add them to the Applet
       
       if (event instanceof RealizeCompleteEvent) 
       	   {
          Component visualComponent;
		if ((visualComponent = player.getVisualComponent()) != null)
             add("Center", visualComponent);
          Component controlComponent;
		if ((controlComponent = player.getControlPanelComponent()) != null)
             add("South",controlComponent);
		
             // force the applet to draw the components
             validate();
             
          }
       else if (event instanceof CachingControlEvent) 
       {

          // Put a progress bar up when downloading starts,
          // take it down when downloading ends.

          CachingControlEvent  e = (CachingControlEvent) event;
          CachingControl      cc = e.getCachingControl();
          long cc_progress       = e.getContentProgress();
          long cc_length         = cc.getContentLength(); 
     
          // Add the bar if not already there ...
          
          Object progressBar = null;
		if (progressBar == null)  
             if ((progressBar = cc.getProgressBarComponent()) != null) 
             {
                add("North", (Component) progressBar);
                validate();
             }
     
          // Remove bar when finished ownloading 
          if (progressBar != null)  
             if (cc_progress == cc_length) 
             {
                remove ((Component) progressBar);
                progressBar = null;
                validate();
             }
       }
       else if (event instanceof EndOfMediaEvent) 
       {
          // We've reached the end of the media; rewind and
          // start over
          
          player.setMediaTime(new Time(0));
          player.start();
          
       }
       else if (event instanceof ControllerErrorEvent) 
       {
          // Tell TypicalPlayerApplet.start() to call it a day
          
          player = null;
          Fatal (((ControllerErrorEvent)event).getMessage());
       }
    }
    void Fatal (String s) 
    {
       // Applications will make various choices about what
       // to do here.  We print a message and then exit
       
       System.err.println("FATAL ERROR: " + s);
       throw new Error(s);  // Invoke the uncaught exception
                            // handler System.exit() is another 
                            // choice
    }


	public String getVd() {
		return vd;
	}

	public void setVd(String vd) {
		this.vd = vd;
	}

}
import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.net.URL;
import java.text.ParseException;

import java.io.*;

import javax.swing.JTextField;

import com.l2fprod.gui.plaf.skin.Skin;
import com.l2fprod.gui.plaf.skin.SkinLookAndFeel;



public class Principal extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	public ExecutaVideo df ;

	private JTextField jTextFieldCodigo = null;

	/**
	 * This is the default constructor
	 */
	public Principal() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(488, 271);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("Menu Principal");
		setResizable(false);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJTextFieldCodigo(), null);
			jContentPane.add(jTextFieldCodigo, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextFieldCodigo	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCodigo() {
		if (jTextFieldCodigo == null) {
			jTextFieldCodigo = new JTextField();
			jTextFieldCodigo.setBounds(new Rectangle(179, 53, 148, 43));
			jTextFieldCodigo.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println(jTextFieldCodigo.getText());
								
					df = new ExecutaVideo();
					df.setVd(jTextFieldCodigo.getText());
					
					df.start();
					df.setVisible(true);
					
				}
			});
		}
		return jTextFieldCodigo;
	}

	public static void main(String args[]) {
	         
	
	
			Principal thisClass = new Principal();
			thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			thisClass.setVisible(true);
			
	 
	   }
}
Criado 27 de junho de 2007
Respostas 0
Participantes 1