/**
 * MP3 Player class
 * 
 * Code based on sample by Steven Haines
 * Original sample code:
 * http://www.informit.com/guides/content.aspx?g=java&seqNum=290
 * 
 * Uses JLayer 1.0 library for MP3 playback
 * http://www.javazoom.net/javalayer/sources.html
 * 
 * Modified by Philip Peng
 * Last updated: 2007/10/09
 * 
 */

// Imports
import javazoom.jl.player.*;
import java.io.*;


public class MP3Player {
	
	// JLayer
	private Player player;
	private InputStream input;
	
	public int position = 0;
	
	/**
	 * Creates a new instance of MP3Player
	 */
	public MP3Player( String filename ) {
	
		try {
			
			// Create an InputStream to the file
			input = new FileInputStream( filename );
			
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Play file
	 */
	
	public void play() {
		
		try {
			
			player = new Player( input );
			PlayerThread pt = new PlayerThread();
			pt.start();
			

			while( !player.isComplete() ) {
				position = player.getPosition();
				System.out.println( "Position: " + position );
				Thread.sleep( 1000 );
			}
			
		} catch( Exception e ) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Playing thread
	 */
	class PlayerThread extends Thread {
		
		public void run() {
			try {
				
				player.play();
				
			} catch( Exception e ) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Just for testing
	 */
	public static void main( String[] args ) {
		
		MP3Player mp3Player = new MP3Player( "C:\\foobar.mp3" );
		mp3Player.play();
		 
	}
}