package djmania.file.audio;

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

/**
 * (Player) Audio player thread for MP3 files.
 * <p>
 * Adds MP3 playback through 
 * <A HREF="http://www.javazoom.net/javalayer/javalayer.html">
 * JLayer MP3 library's Player class</A>. 
 * Used by the {@link djmania.file.AudioReader} class.
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Philip Peng
 * @version	1.0 27-Oct-07
 * 
 */

public class MP3PlayerThread extends AudioPlayerThread {

	private Player player;
	
	public int setStream(String file) {
		try {
			FileInputStream stream;
			stream = FileTools.convertStringToStream(file);
			player = new Player(stream);
			return 1; // Success
		} catch ( java.io.FileNotFoundException e ) {
			return -1; // Fail: file not found
		} catch ( javazoom.jl.decoder.JavaLayerException e ) {
			return 0; // Fail: unable set stream
		}
	}
	
	public int getPosition() {
		return player.getPosition();
	}
	
	public void pausePlayback() {
		// Uses Thread's depreciated method
		// as JLayer's Player class doesn't
		// have an equivalent
		this.suspend();
	}
	
	public void resumePlayback() {
		// Uses Thread's depreciated method
		// as JLayer's Player class doesn't
		// have an equivalent
		this.resume();
	}
	
	public void close() {
		player.close();
	}
	
	public void run() {
		try {
			player.play();
			player.close();
		} catch ( Exception e ) {
			e.getMessage();
		}
	}
	
}
