package tmaestroplayer;

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.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.Time;
import javax.swing.JPanel;

public class MediaPanel extends JPanel {

    public Player mediaPlayer = null;
    String locator = "";
    //SSS 26102009 dos variables
    URL mediaURL;
    String player;

    /*
     * Gestion de reproductor multimedia
     */
    public MediaPanel(URL mediaURL, String player) {
        //SSS 26102009 variables globales
        this.mediaURL=mediaURL;
	this.player=player;
	locator=player;
        setLayout(new BorderLayout());
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));

        try {
            mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            Component video = mediaPlayer.getVisualComponent();
            if (video != null) {
                add(video, BorderLayout.CENTER);
            }
        } catch (NoPlayerException noPlayerException) {
            System.err.println("No media player found");
        } catch (CannotRealizeException cannotRealizeException) {
            System.err.println("Could not realize media player");
        } catch (IOException iOException) {
            System.err.println("Error reading from the source");
        }
    }

    public void start() {
        //SSS 26102009
        if (mediaPlayer != null) {
            mediaPlayer.start();
        }
    }

    public void stop() {
        //SSS 26102009
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.setMediaTime(new Time(0));
        }
    }

    public void pause() {
        //SSS 26102009
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.setMediaTime(mediaPlayer.getMediaTime());
        }
    }

    public double getTime() {
        return mediaPlayer.getMediaTime().getSeconds();
    }

    public void setTime(double seconds) {
        mediaPlayer.setMediaTime(new Time(seconds));
    }

    public double getDuration() {
        return mediaPlayer.getDuration().getSeconds();
    }
} 

