AudioPlayer.java :  » Testing » jacareto » jacareto » replay » mediaplayer » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » replay » mediaplayer » AudioPlayer.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.replay.mediaplayer;


import java.io.File;
import java.io.IOException;

import java.net.MalformedURLException;

import javax.media.ControllerEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;

/**
 * Player for audio files.
 *
 * @author Oliver Specht
 * @version $revision$
 */
public class AudioPlayer extends MediaPlayer {
    /** The Player instance to replay audio files */
    Player player;

    /**
     * Creates a new AudioPlayer and initiatlizes it
     */
    public AudioPlayer () {
        super();
    }

    /**
     * Creates a new AudioPlayer with the given audio file
     *
     * @param filename The audio file this player should replay
     */
    public AudioPlayer (String filename) {
        super(filename);
    }

    /**
     * Destroys this audio player (sets it to null)
     */
    public void destroy () {
        player.deallocate ();
        player = null;
    }

    /**
     * Called when the {@link jacareto.cleverphl.session.Session} stops
     */
    public void stop () {
        this.player.stop ();
    }

    /**
     * Called when the {@link jacareto.cleverphl.session.Session} stops
     */
    public void pause () {
        this.player.stop ();
    }

    /**
     * {@inheritDoc}
     */
    protected void init () {
        try {
            // create audio player
            if ((new File(this.getFilename ())).exists ()) {
                player = Manager.createPlayer (new MediaLocator("file:" + this.getFilename ()));
            } else {
                // load as resource from classpath
                // does not work for jars, because JMF cannot load audio files from jars
                player = Manager.createPlayer (new MediaLocator(getClass ().getResource ("/" +
                                this.getFilename ())));
            }
        } catch (MalformedURLException e) {
            e.printStackTrace ();
            destroy ();
        } catch (NoPlayerException e) {
            e.printStackTrace ();
            destroy ();
        } catch (IOException e) {
            e.printStackTrace ();
            destroy ();
        }

        player.addControllerListener (this);
        player.realize ();
    }

    /**
     * Replays the audio file
     *
     * @return true, if replay is successful
     */
    public boolean replay () {
        player.start ();

        return true;
    }

    /**
     * Returns the wrapped {@link Player} instance of this AudioPlayer
     *
     * @return Player this player
     */
    public Player getPlayer () {
        return this.player;
    }

    /**
     * {@inheritDoc}
     */
    protected void processPrefetchCompleteEvent (ControllerEvent event) {
    }

    /**
     * {@inheritDoc}
     */
    protected void processRealizeCompleteEvent (ControllerEvent event) {
        this.player.prefetch ();
    }

    /**
     * {@inheritDoc}
     */
    protected void processEndOfMediaEvent (ControllerEvent event) {
        this.isRunning = false;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isRunning () {
        if (this.getPlayer ().getState () == Player.Started) {
            return true;
        }

        return false;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.