package djmania.sandbox.old;

import javazoom.jl.player.Player;
import java.io.FileInputStream;
import djmania.sandbox.old.FileTools;

/**
 * MP3 player for multi-threading.
 * <p>
 * Uses <A HREF="http://www.javazoom.net/javalayer/sources.html">
 * JLayer MP3 library's Player class</A> for MP3 playback. 
 * Better for multi-threading usage over the {@link PlayerThreaded} class.
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Philip Peng
 * @version	2.0 Oct 25, 2007
 * @deprecated
 * 
 */

public class ThreadedPlayer extends Thread {
	
	// JLayer Player object
	private Player foobar;
	
	public ThreadedPlayer() {} // End constructor()
	
	public ThreadedPlayer(FileInputStream stream) {
		this.setStream(stream);
	} // End constructor(FileInputStream)
	
	public ThreadedPlayer(String file) {
		this.setStream(file);
	} // End constructor(String)
	
	/**
	 * Sets playback input stream.
	 * @param	stream	File stream (FileInputStream object).
	 * @return	0 if failed, 1 if successful.
	 */
	public int setStream (FileInputStream stream) {
		try {
			foobar = new Player(stream);
			return 1; // Success
		} catch ( javazoom.jl.decoder.JavaLayerException e ) {
			return 0; // Fail: unable set stream
		}
	} // End setStream(FileInputStream) method
	
	/**
	 * Sets playback input stream.
	 * @param	file	File location (String object).
	 * @return	0 if failed, -1 if file not found, 1 if successful.
	 */
	public int setStream (String file) {
		try {
			return this.setStream(FileTools.convertStringToStream(file));
		} catch ( java.io.FileNotFoundException e ) {
			return -1; // Fail: file not found
		}
	} // End setStream(String) method

	
	/**
	 * Returns current playback position.
	 * @return	current position in milliseconds
	 */
	public int getPosition() {
		return foobar.getPosition();
	} // End getPosition method
	
	/**
	 * Starts playback. 
	 * Use .suspend() and .resume() methods to pause and resume.
	 */
	public void run() {
		try {
			foobar.play();
			foobar.close();
		} catch ( Exception e ) {
			e.getMessage();
		}
	} // End run method

}
