Problema p/ trocar de vídeo - JMF(Java Media Framework)

2 respostas
Allan_BSO

Pessoal,
Gostaria mais uma vez contar com o apoio de todos. Estou tentando fazer um player usando JMF(Java Media Framework), no entanto estou enfrentando algumas dificuldades, como por exemplo, ao tentar abrir um outro vídeo (NO MENU FILE --> ABRIR), o player simplesmente deixa ambos os vídos carregados no controller... e não estou achando uma solução de encerrar o outro e exibir somente o segundo vídeo passado para a URL...

Não estou conseguindo entender o que estou fazendo de errado...

Sou novato ainda em Java e precisa de uma força... quem puder ajudar... :wink:

Segue o código Fonte:

//Classe PlayerSM. Cria o frame e carrega o painel...

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

public class PlayerSM {
	
	public static void main(String args[]){
		
		JFileChooser fileChooser = new JFileChooser();
		
		int result = fileChooser.showOpenDialog(null);
		
		if(result == JFileChooser.APPROVE_OPTION){
			
			URL mediaURL = null;
			
			try{
				mediaURL = fileChooser.getSelectedFile().toURL();
				
			}catch(MalformedURLException malformedURLException){
				JOptionPane.showMessageDialog(null, "Houve um erro para abrir o arquivo!");
				System.err.println("Houve um erro para abrir o arquivo!\nErro: malformedURLException");
			}
			
			if(mediaURL != null){
				
				JFrame frameSMPlayer = new JFrame("SM Player");
				frameSMPlayer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
				PlayerPanel playerPanel = new PlayerPanel(mediaURL);
				frameSMPlayer.add(playerPanel);
				
				frameSMPlayer.setSize(400,300);
				frameSMPlayer.setResizable(true);
				frameSMPlayer.setVisible(true);	
				
				
				
			}
			
		}else{
			JOptionPane.showMessageDialog(null, "Nenhum Arquivo Selecionado. Sistema será encerrado...");
			System.exit(0);
		}
		
		
	}
}

Classe PlayerPanel: Carrega os Objetos e o Player em si.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.media.*;

public class PlayerPanel extends JPanel{

	private JMenuBar menuBar = new JMenuBar();
	private JMenu fileMenu = new JMenu("Menu");
	private JMenuItem subMenuAbrirMidia = new JMenuItem("Abrir...");
	
	private Player mediaPlayer;
	private Component video;
	private Component controls;

	
	public PlayerPanel (URL mediaURL){
		
		this.setLayout(new BorderLayout());
		
		Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
		
		System.out.println(mediaURL);
		
		try{
			mediaPlayer = Manager.createRealizedPlayer(mediaURL);
			video = mediaPlayer.getVisualComponent();
			controls = mediaPlayer.getControlPanelComponent();
			
					
			subMenuAbrirMidia.addActionListener(new ActionListener(){
				
				public void actionPerformed(ActionEvent e) {
					if(e.getSource() == subMenuAbrirMidia){
						mediaPlayer.stop();
						mediaPlayer.setMediaTime(new Time(0));
						mediaPlayer.deallocate();
						
						JFileChooser fileChooser = new JFileChooser();
						
						int result = fileChooser.showOpenDialog(null);
						
						if(result == JFileChooser.APPROVE_OPTION){
							
							URL mediaURL = null;
							
							try{
								mediaURL = fileChooser.getSelectedFile().toURL();
								
							}catch(MalformedURLException malformedURLException){
								JOptionPane.showMessageDialog(null, "Houve um erro para abrir o arquivo!");
								System.err.println("Houve um erro para abrir o arquivo!\nErro: malformedURLException");
							}
							
							Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
							
							try{
							
								mediaPlayer = Manager.createRealizedPlayer(mediaURL);
								video = mediaPlayer.getVisualComponent();
								controls = mediaPlayer.getControlPanelComponent();
								
								mediaPlayer.start();
								
							}catch(NoPlayerException noPlayerException){
								System.err.println(noPlayerException);
							}catch(CannotRealizeException cannotRealizeException){
								System.err.println(cannotRealizeException);
							}catch(IOException iOException){
								System.err.println(iOException);
							}
						}
					}
					
				}
				
			});
			
			fileMenu.add(subMenuAbrirMidia);
			menuBar.add(fileMenu);
			this.add(menuBar, BorderLayout.NORTH);
			
			if(video!=null)this.add(video, BorderLayout.CENTER);
			if(controls!=null)	this.add(controls, BorderLayout.SOUTH);
					
			mediaPlayer.start();
			
		}catch(NoPlayerException noPlayerException){
			System.err.println(noPlayerException);
		}catch(CannotRealizeException cannotRealizeException){
			System.err.println(cannotRealizeException);
		}catch(IOException iOException){
			System.err.println(iOException);
		}
	}
}

Se alguem conseguir me ajudar ficaria grato...
Se alguém souber como posso usar o listener para zerar o controller e exibir o novo conteúdo NO MENU ABRIR...

Fico grato...

:wink:

2 Respostas

Allan_BSO

Alguem???

D

amigo me diga, quando vc abre o video aparece o video memso ou so o audio, caso o video apareça qual o formato que vc esta abrindo.

eu tenho um aplicação igual so que vou retirar foto ( no caso frames) do video so que a droga so abre o audio, isso aconteceu com vc, pesquisando descobri que é pq o metodo getVisualComponente() da class player esta retornando null, me pesquisando no site da sun um topico dizia que era pq so abria videos sem audio ( axei estupidez, tipo que droga de API é essa0 e nao era isso abri um videos sem audio e nem executou, deu erro.

os videos que tento abrir estao em formato mpeg, tentei abrir avi e deu erro, apesar de que no site official da JMF dizer que ela suporta este formato???

hellllp

Criado 8 de novembro de 2009
Ultima resposta 3 de jun. de 2010
Respostas 2
Participantes 2