import javax.media.*;
import java.io.File;
import java.awt.*;
import javax.swing.RootPaneContainer;
import java.io.IOException;
import java.net.MalformedURLException;
public class JMFPlayers implements ControllerListener
{
//f: Das Musik-File
//wait: Auf Musikende warten
//Die Methoden deallocate und close werden nicht aus dieser Methode ausgeführt.
public static Player playMusic (File f, boolean wait) throws IOException, NoPlayerException
{
try{
Player player = Manager.createPlayer (f.toURL()); //1
player.start(); //2
if (wait){
waitForMedia (player);
}
return player;
}catch (MalformedURLException e){return null;}
}
//f: Das Video oder Musik-File
//c: Zu diesem Container wird das VisualComponent, falls vorhanden, hinzugefügt
//constraints: mit diesen constraints
//wait: Auf Ende warten
public static Player playVideo (File f, final Container c, final Object constraints, boolean wait) throws IOException, NoPlayerException
{
try{
final Player player = Manager.createPlayer (f.toURL()); //1
player.addControllerListener (new ControllerListener(){ //5
public synchronized void controllerUpdate(ControllerEvent event) {
if (event instanceof RealizeCompleteEvent) {
Component comp;
if ((comp = player.getVisualComponent()) != null){
if (c instanceof RootPaneContainer)
((RootPaneContainer)c).getContentPane().add (comp, constraints);
else
c.add (comp);
}
c.validate();
}
}
});
player.start(); //2
if (wait){
waitForMedia (player);
}
return player;
}catch (MalformedURLException e){return null;}
}
private static void waitForMedia (Player p)
{
JMFPlayers jp = new JMFPlayers();
p.addControllerListener (jp);
while (!jp.stopped){ //7
try{
Thread.sleep (800);
}catch (InterruptedException e){}
}
p.stop(); //3
}
private JMFPlayers(){}
private boolean stopped = false;
public synchronized void controllerUpdate(ControllerEvent event)
{
if (event instanceof StopEvent && !(event instanceof StopByRequestEvent || event instanceof RestartingEvent)){
stopped = true;
}
}
}