package IO;
import java.io.InputStream;
import java.util.Map;
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 GUI.Submasks.ActualMusicList;
import GUI.Submasks.MusicPlayer;
import GUI.Submasks.MyPlayList;
import UTIL.Utilities;
public class MediaPlayer implements BasicPlayerListener{
private InputStream inputstream = null;
// Instantiate BasicPlayer.
public static BasicPlayer player = new BasicPlayer();
// BasicPlayer is a BasicController.
BasicController control = (BasicController) player;
public MediaPlayer(){
player.addBasicPlayerListener(this);
}
private int filelength = 0;
private InputStream in = null;
//Currently playlist 1= ExtendedPlaylist, 2 = actualplaylist
public static int currently_playlist = -1;
//must be true if next song is choosen manually
private static boolean manuelNextTrack = false;
public void play(InputStream ins, int filelength, boolean manuelNextTrack)
{
MediaPlayer.manuelNextTrack = manuelNextTrack;
MusicPlayer.musicFrames.removeAllElements();
player.setLineBufferSize(409600);
this.filelength = filelength;
try
{
in = ins;
if(player.getStatus()==player.PLAYING)
control.stop();
control.open(ins);
// Start playback in a thread.
control.play();
control.setGain(MusicPlayer.getGainValue());
control.setPan(0.0);
}
catch (BasicPlayerException e)
{
e.printStackTrace();
}
}
private boolean pauseWasPressed = false;
public void pause(){
try {
if(pauseWasPressed){
control.resume();
pauseWasPressed = false;
}
else{
control.pause();
pauseWasPressed = true;
}
} catch (BasicPlayerException e) {
e.printStackTrace();
}
}
public void setGain(double val){
try {
control.setGain(val);
} catch (BasicPlayerException e) {
e.printStackTrace();
}
}
public void stopWhileDownload(){
try{
if(player.getStatus()!=player.STOPPED || player.getStatus()!=player.PAUSED){
if(player.getStatus()==player.PLAYING)
player.pause();
else
player.resume();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void seek(long seek){
try {
control.seek(seek);
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop(){
try {
Utilities.musicscroller.stopScrolling();
control.stop();
} catch (BasicPlayerException e) {
e.printStackTrace();
}
}
public void opened(Object stream, Map properties)
{
display("opened : "+properties.toString());
}
long b = 0;
public void progress(int bytesread, long microseconds, byte[] pcmdata, Map properties)
{
// Pay attention to properties. It depends on underlying JavaSound SPI
// MP3SPI provides mp3.equalizer.
//properties.get(key)
//System.out.println(((Long.parseLong((String)properties.get("mp3.position.microseconds"))/1000)/1000));
try{ //mp3.position.microseconds
b = ((Long)properties.get("mp3.position.byte")).longValue();
// System.out.println((filelength-(((Long)properties.get("mp3.frame")).longValue()*((Integer)properties.get("mp3.frame.size.bytes")).intValue()))/((Integer)properties.get("mp3.frame.size.bytes")).intValue());
MusicPlayer.setSliderValue((int)((((float)b/(float)filelength)*100)),b);
}catch(Exception e){
e.printStackTrace();
}
//display("progress : "+properties.toString());
}
public void stateUpdated(BasicPlayerEvent event)
{
// Notification of BasicPlayer states (opened, playing, end of media, ...)
display("stateUpdated : "+event.toString());
if(event.getCode() == BasicPlayerEvent.STOPPED){
System.out.println(manuelNextTrack);
if(currently_playlist>0 && MusicPlayer.playlistmode.isSelected() && !manuelNextTrack){
switch(currently_playlist){
case 1: MyPlayList.playNextSong(); break;
case 2: ActualMusicList.playNextSong(); break;
}
}
}else if(event.getCode() == BasicPlayerEvent.PLAYING && manuelNextTrack!=false)
manuelNextTrack = false;
}
public void setController(BasicController controller)
{
display("setController : "+controller);
}
public void display(String msg)
{
System.out.println(msg);
// if (out != null) out.println(msg);
}
}