Load another video
Well, I'm making a video player, I got this trouble...
I can load a video, but when I want to load another one, I have to clean the video panel, so this makes my UI blinking (I'm not sure if this is the correct word). I think the reason is that I create another Player with Manager, I mean I kill the first one ( player.close(), player.stop(), player.deallocate() ).... but I only want is to change the file, I mean "the video", if some one can help me please, or maybe an example ... I really need it... this is the code I have:
publicvoid play(){
try{
// this is changed....
String selected ="someVideo.mpg";
file =new File(selected);
if(file.exists()){
if( player!=null ){
player.stop();
player.deallocate();
player.close();
panelVideo.removeAll();
}
URL url=file.toURL();
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,new Boolean(true));
player = Manager.createPlayer(url);
player.addControllerListener(eventoAction);
player.start();
}else
System.out.println("File not founded");
}catch(Exception e){}
}
I really will be thanking you... Wilfredo Vargas Almendras
[1922 byte] By [
Wilfredoa] at [2007-10-3 5:05:51]

I don't think you can get away from this as you are having to get a new Player for each file/video
How about a 'splash' screen saying "new video loading, please wait"
You can put this up at the EndOfMediaEvent and tear it down at the RealizedEvent after which you can get the visualComponent from the Player to show in your panel
The JWindows o somehting like you say, is a good idea. But I'd like to change my video file,, please I tried many things, but I couldn't do it.Any way, thank you
Change your play method to take in the File as a parameter - something like
public void play(File f){
try{
if(f.exists()){
{
URL url=file.toURL()
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
player = Manager.createPlayer(url);
player.addControllerListener(eventoAction);
player.start();
.......
Now the first call must pass in the name of your first file
You will need to set up a Listener to catch the EndOfMediaEvent - close down the current Player
if( player!=null ){
player.stop();
player.deallocate();
player.close();
panelVideo.removeAll();
}
and call your Play method with the new/next File
That's what I'm doing, but there is a problem, when I change the video file, my video visor blinks, and then start the new video. I want this bug get away. Please, help me.
Can you post up some of your codeI don't think you are going to be able to eliminate a screen flicker completely
I really appreciate your help, this is part of my code:
public class IU_PanelVideo extends JPanel{
protected Player player;
protected File file;
protected JPanel panel = new JPanel();
protected JPanel panelVideo = new JPanel();
protected String selected;
protected JFrame parent;
public IU_PanelVideo(JFrame pa){
parent = pa;
....
parent.pack();
}
public void eventoControllerUpdate( ControllerEvent e ){
if(e instanceof RealizeCompleteEvent){
if(player.getVisualComponent()!=null)
panelVideo.add(player.getVisualComponent(),BorderLayout.CENTER);
panelVideo.add(player.getControlPanelComponent(),BorderLayout.SOUTH);
parent.pack();
}
if(e instanceof EndOfMediaEvent){
play();
}
}
class EventoAction implements ActionListener,ControllerListener{
public void actionPerformed(ActionEvent e){
..........................
}
public void controllerUpdate(ControllerEvent e){
..................
}
}
public void play(){
try{
// Selected is changed by event -... this is changed
file = new File(selected);
if(file.exists()){
if( player!=null ){
player.stop();
player.deallocate();
player.close();
panelVideo.removeAll();
}
URL url=file.toURL();
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
player = Manager.createPlayer(url);
player.realize();
player.start();
}else
System.out.println("File not found");
}catch(Exception e){}
}
}
I have another class inheret from this, but my trouble is in the play method...
please help me...
No body can help me .... please
Take a look at the following - seems to work
or is your screen flicker worse than this
Hope this helps
/*
* MyPlayer3.java
*
*Copyright Stafah Ltd
*
*This code may be used as is, for personal / educational purposes only
*Stafah accepts no liabilities for any problems arising from the use of this code
*
*/
package com.stafah.jmf;
import javax.media.*;
import com.sun.media.ui.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
/**
*
* @author Stefan
*/
public class MyPlayer3 extends JFrame implements ActionListener,ItemListener,ControllerListener
{
JPanel jP = new JPanel();
Boolean startCheck;
Boolean loopSwitch;
JButton openButton;
JCheckBox loopButton;
JFileChooser fc;
JButton jBtnStop = new JButton("S");
JButton jBtnGo = new JButton("G");
//Player audioPlayer;
Player player;
Component visual = null;
Component controls;
Container c = new Container();
Vector vFiles;
Iterator iTFiles;
public MyPlayer3()
{
super("MyPlayer3");
c = getContentPane();
jP.setLayout(new BorderLayout());
c.add(jP);
startCheck = false;
loopSwitch = false;
loopButton = new JCheckBox("Loop");
loopButton.setMnemonic(KeyEvent.VK_L);
loopButton.setSelected(false);
loopButton.addItemListener(this);
fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
openButton = new JButton("Open a file");
openButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(loopButton);
jP.add(buttonPanel, BorderLayout.NORTH);
jBtnStop.addActionListener(this);
buttonPanel.add(jBtnStop);
jBtnGo.addActionListener(this);
buttonPanel.add(jBtnGo);
pack();
setVisible(true);
}
private void performStop()
{
if(startCheck == true)
{
player.stop();
}
}
private void performGo()
{
if(startCheck == true)
{
player.start();
}
}
private void playFile()
{
File file = (File)iTFiles.next();
System.out.println("About to show '" + file.getName() + "'");
if(startCheck == true)
{
player.removeControllerListener(this);
player.close();
if(visual != null)
{
jP.remove(visual);
}
if(controls != null)
{
jP.remove(controls);
}
startCheck = false;
}
try
{
player = Manager.createPlayer(new MediaLocator("file:///" + file));
player.addControllerListener(this);
player.realize();
}
catch(Exception eX)
{
eX.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if(obj == jBtnStop)
{
performStop();
}
else if(obj == jBtnGo)
{
performGo();
}
else if(obj == openButton)
{
int returnVal = fc.showOpenDialog(MyPlayer3.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File [] files = fc.getSelectedFiles();
vFiles = new Vector();
for(int i = 0; i < files.length; i++){
vFiles.add(files[i]);
}
iTFiles = vFiles.iterator();
playFile();
}
}
}
public void itemStateChanged(ItemEvent g)
{
Object source=g.getItemSelectable();
if(source==loopButton)
{
if(g.getStateChange()==ItemEvent.SELECTED)
{
loopSwitch=true;
}
else if(g.getStateChange()==ItemEvent.DESELECTED)
{
loopSwitch=false;
}
}
}
public void controllerUpdate(ControllerEvent event)
{
if(event instanceof RealizeCompleteEvent)
{
player.prefetch();
}
else if(event instanceof EndOfMediaEvent && loopSwitch)
{
if(!iTFiles.hasNext() && loopSwitch){
iTFiles = vFiles.iterator();
}
playFile();
}
else if(event instanceof PrefetchCompleteEvent)
{
if((visual = player.getVisualComponent()) != null)
{
jP.add(visual, BorderLayout.CENTER);
}
if((controls = player.getControlPanelComponent()) != null)
{
jP.add(controls, BorderLayout.SOUTH);
}
player.start();
startCheck=true;
}
else if(event instanceof StartEvent)
{
pack();
}
}
public static void main(String args[])
{
new MyPlayer3();
}
}
CODE correction in
public void controllerUpdate(ControllerEvent event)
else if(event instanceof EndOfMediaEvent)
{
if(loopSwitch){
iTFiles = vFiles.iterator();
}
playFile();
}
**** - that causes a different fault at end of list of Files Will post correction shortly
Code correction in
public void controllerUpdate(ControllerEvent event)
else if(event instanceof EndOfMediaEvent)
{
if(iTFiles.hasNext()){
playFile();
}
else{
if(loopSwitch){
iTFiles = vFiles.iterator();
playFile();
}
}
}
I really thanks for your help, it works better this way. I mean my UI was blinking much more, but now it looks better, anything I will post...Thank you again...Wilfredo Vargas