0% found this document useful (0 votes)
272 views

Javazoom Library

This class implements an MP3 audio player with playback controls. It uses the BasicPlayer class to handle playback and extends BasicPlayerListener to receive playback events. Methods are provided to play, pause, stop, seek to positions, and change the volume. The player notifies any registered MediaPlayerListener objects of playback events.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
272 views

Javazoom Library

This class implements an MP3 audio player with playback controls. It uses the BasicPlayer class to handle playback and extends BasicPlayerListener to receive playback events. Methods are provided to play, pause, stop, seek to positions, and change the volume. The player notifies any registered MediaPlayerListener objects of playback events.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

package sepm.juicer.

audio;
import java.io.File;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javazoom.jlgui.basicplayer.BasicController;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerEvent;
import javazoom.jlgui.basicplayer.BasicPlayerException;
import javazoom.jlgui.basicplayer.BasicPlayerListener;
import sepm.juicer.audio.listener.MediaPlayerListener;
import sepm.juicer.bo.MP3Data;
/**
*
* This Class is the basic class of the Audioplayer implementation. <br />
* It doesn't include any of Gui Component. On this Class will be only the
* functional methods of an Audio included.
*
* <br />
*
* Futures are : <br />
* play , stop , seek , Volume(up-down)
*
* <br />
*
* To see for more information Methods declarations.
*
* @author Hasan Oezdemir
* @version 1.0
*
*/
public class MP3Player implements BasicPlayerListener , MediaPlayerListener {
private BasicPlayer player = null;
private ArrayList< MediaPlayerListener > listener = null;
private float volume = 0.3F;
private long curPos = 0;
private boolean stopped = false;
private boolean seeking = false;
private MP3Data data;
private JSlider slider;
private JLabel actualTime;
/**
* Default Constructor.
*
* @param slider
*/
public MP3Player( JSlider slider , JLabel actualTime ) {
this.slider = slider;
this.actualTime = actualTime;
this.listener = new ArrayList< MediaPlayerListener >();
player = new BasicPlayer();
player.addBasicPlayerListener( this );
}
/**
*
* @param mpl
*/
public void addMediaPlayerListener( MediaPlayerListener mpl ) {
listener.add( mpl );
}
/**
*
* @return the actual Position of the Song
*/
public long getPosition() {
return curPos;
}
/**
*
* @return the actual Status of Song. It could be such as ( play , stop
)
* but with an Integer constant
*/
public int getState() {
return player.getStatus();
}
/**
*
* @return the actual Volume.
*/
public float getVolume() {
return volume;
}
public void opened( Object arg0 , Map arg1 ) {
}
/**
*
* @throws BasicPlayerException
*/
public void pause() throws BasicPlayerException {
player.pause();
}
/**
* Plays the actual Song on the Buffer. <br />
* This Method plays a Song with Position '0(zero)'
*
* @throws BasicPlayerException
*/
public void play() throws BasicPlayerException {
play( 0 );
}
/**
* Plays actual Song with Skipping the Seconds.
*
* @param seconds
* @throws BasicPlayerException
*/
public void play( long seconds ) throws BasicPlayerException {
stop();
if ( data == null )
throw new BasicPlayerException( "Kein MP3 gefunden" );
File f = new File( data.getPath() );
if( !f.exists() )
throw new BasicPlayerException( "MP3 nicht gefun
den." );
player.open( f );
setPosition( seconds );
player.play();
setCurVolume();
}
/*
* (non-Javadoc)
*
* @see javazoom.jlgui.basicplayer.BasicPlayerListener#progress(int, lon
g,
* byte[], java.util.Map)
*/
public void progress( int arg0 , long microseconds , byte [] arg2 , Map
arg3 ) {
slider.setValue( ( int ) microseconds / 1000 );
Time time = new Time( microseconds / 1000 );
actualTime.setText( time.toString().substring( 3 ) );
}
/**
*
* @param mpl
*/
public void removeMediaPlayerListener( MediaPlayerListener mpl ) {
listener.remove( mpl );
}
/**
*
* @throws BasicPlayerException
*/
public void resume() throws BasicPlayerException {
player.resume();
}
/**
*
* @param byt
* @throws BasicPlayerException
*/
public void seek( long byt ) throws BasicPlayerException {
player.seek( byt );
}
/*
* (non-Javadoc)
*
* @see
* javazoom.jlgui.basicplayer.BasicPlayerListener#setController(javazoom
* .jlgui.basicplayer.BasicController)
*/
public void setController( BasicController arg0 ) {
}
/**
* Sets the current Volume.
*
* @see #setVolume(float)
*
* @throws BasicPlayerException
*/
private void setCurVolume() throws BasicPlayerException {
if ( player != null && player.getStatus() != BasicPlayer.STOPPED
&& player.hasGainControl() ) {
player.setGain( volume );
}
}
/**
* Sets a MP3Data MusicData , that will be played.
*
* @param data
* @throws BasicPlayerException
*/
public void setData( MP3Data data ) throws BasicPlayerException {
this.data = data;
}
/**
* Sets the Position of actual Song.
*
* @see #seek(long)
* @param pos
* @throws BasicPlayerException
*/
public void setPosition( long pos ) throws BasicPlayerException {
if ( player != null && player.getStatus() != BasicPlayer.STOPPED
) {
seeking = true;
player.seek( pos );
seeking = false;
}
}
/**
* Sets , which of Slider will be used. <br />
* It should be a Slider for showing the currently Position of Song.
*
* @param slider
*/
public void setSlider( JSlider slider ) {
this.slider = slider;
}
/**
*
* @param stopped
*/
public void setStopped( boolean stopped ) {
this.stopped = stopped;
}
/**
* Sets Volume up or down.
*
* @param volume
* @throws BasicPlayerException
*/
public void setVolume( float volume ) throws BasicPlayerException {
this.volume = volume;
setCurVolume();
}
/**
*
*/
@Override
public void songEnded() {
}
/**
* If the Song finished , will be all of MediaplayerListeners notified.
*/
public void songFinished() {
for ( MediaPlayerListener mpl : listener ) {
mpl.songEnded();
}
}
/*
* (non-Javadoc)
*
* @see
* javazoom.jlgui.basicplayer.BasicPlayerListener#stateUpdated(javazoom.
* jlgui.basicplayer.BasicPlayerEvent)
*/
public void stateUpdated( BasicPlayerEvent arg0 ) {
if ( player.getStatus() == BasicPlayer.STOPPED && stopped == fal
se
&& seeking == false ) {
songFinished();
stopped = true;
} else if ( stopped == true
&& player.getStatus() == BasicPlayer.PLAYING ) {
stopped = false;
}
}
/**
* Stops the Song.
*
* @throws BasicPlayerException
*/
public void stop() throws BasicPlayerException {
if ( player != null && player.getStatus() != BasicPlayer.STOPPED
)
{
player.stop();
}
}
}

You might also like