This is a program i wrote to implement play/pause,stop,next,previous,fastforward on a list of three songs.
The two statements highlighted in red towards the end of the program dont get executed at all..
Can anyone plz xpain why it is so?
When play is pressed the button goes invisible and pause button gets visible..
What i wanted was that while playing if i press fastforward, then the pause button shud be removed and play button made visible. But the two statements remain unexecuted and the fast forward begins..
Plz help.. Thanx..
package com.android.google.musicplayer;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
public class MusicPlayer extends Activity {
/** Called when the activity is first created. */
MediaPlayer mp;
int currentSong;
ImageButton PAUSE,PLAY;
int flag=0;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final int filz[]= {R.raw.rock,R.raw.stone,R.raw.shawn};
final ListView lv = (ListView)this.findViewById(R.id.widget1);
PAUSE=(ImageButton)this.findViewById(R.id.pAuse);
PAUSE.setVisibility(4);
PAUSE.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{ mp.pause();
PAUSE.setVisibility(4);
PLAY.setVisibility(0);
flag=1;
}
});
PLAY=(ImageButton)this.findViewById(R.id.pLay);
PLAY.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{ if(flag==1)
{ mp.start();
flag=0;
}
else
if(flag==2)
{ mp.pause();
mp.start();
flag=0;
}
else{
int x=lv.getSelectedItemPosition();
currentSong = x;
mp = MediaPlayer.create(MusicPlayer.this, filz[x]);
try{mp.prepare();}catch(IOException e){}
mp.start();
}
PAUSE.setVisibility(0);
PLAY.setVisibility(4);
}
});
ImageButton STOP=(ImageButton)this.findViewById(R.id.sTop);
STOP.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{ mp.stop();
PAUSE.setVisibility(4);
PLAY.setVisibility(0);
}
});
ImageButton PREV=(ImageButton)this.findViewById(R.id.pRev);
PREV.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{ mp.stop();
if(currentSong>=1)
{ currentSong=currentSong-1;
mp = MediaPlayer.create(MusicPlayer.this, filz[currentSong]);
try{mp.prepare();}catch(IOException e){}
mp.start();
}
}
});
ImageButton NEXT=(ImageButton)this.findViewById(R.id.nExt);
NEXT.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{ mp.stop();
if(currentSong<=1)
{ currentSong=currentSong+1;
mp = MediaPlayer.create(MusicPlayer.this, filz[currentSong]);
try{mp.prepare();}catch(IOException e){}
mp.start();
}
}
});
ImageButton FFOR=(ImageButton)this.findViewById(R.id.fFor);
FFOR.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{ int curPos,fulDur;
flag=2;
fulDur=mp.getDuration();
PAUSE.setVisibility(4);
PLAY.setVisibility(0);
try {Thread.sleep(2000);}catch(Exception e){}
do{
curPos = mp.getCurrentPosition();
mp.seekTo(curPos+2000);
try{Thread.sleep(1000);}catch(Exception e){}
}while((curPos+2000)<fulDur);
}
});
}
}
PS: i dunno why but the code isnt being indented as it is supposed to be... sorry..


