我是Android开发领域的新秀。我正在尝试构建音乐播放器应用程序。如果我删除
行上的catch块,该程序将给出一个IllegalStateException
currentPosition = myMediaPlayer.getCurrentPosition();
代码:
updateSeekBar = new Thread(){
@Override
public void run(){
int totalDuration = myMediaPlayer.getDuration();
int currentPosition = 0;
while(currentPosition<totalDuration)
{
try {
sleep(200);
currentPosition = myMediaPlayer.getCurrentPosition();
songSeekBar.setProgress(currentPosition);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (IllegalStateException e)
{
return;
}
}
}
};
if(myMediaPlayer!=null) {
myMediaPlayer.stop();
myMediaPlayer.release();
}
String name = getIntent().getStringExtra("SongName");
String[] namearray = new String[name.length()];
namearray = m.WordsUsingSplit(name);
label.setText(namearray[0]);
label.setSelected(true);
mySongNameList = getIntent().getStringArrayListExtra("SongsList");
Bundle bundle=getIntent().getExtras();
mySongs = (ArrayList) bundle.getParcelableArrayList("PathList");
song = mySongs.get(position).getName();
position = bundle.getInt("pos",0);
play(position);
/*
images = getIntent().getStringArrayListExtra("coverArt");
information = (TextView) findViewById(R.id.info);
information.setText(images.get(position).toString());
File f = new File(images.get(position));
Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath());
imageView = (ImageView) findViewById(R.id.cover_image);
imageView.setImageBitmap(bm);
*/
songSeekBar.setMax(myMediaPlayer.getDuration());
updateSeekBar.start();
songSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
myMediaPlayer.seekTo(seekBar.getProgress());
}
});
bpause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
songSeekBar.setMax(myMediaPlayer.getDuration());
if(myMediaPlayer.isPlaying()){
bpause.setBackgroundResource(R.drawable.play_icon);
myMediaPlayer.pause();
}
else
{
bpause.setBackgroundResource(R.drawable.pause_icon);
myMediaPlayer.start();
}
}
});
bnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.stop();
myMediaPlayer.release();
position = (position+1)%mySongs.size();
play(position);
}
});
bprev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.stop();
myMediaPlayer.release();
position = (((position-1)<0)?(mySongs.size()-1):(position-1));
play(position);
}
});
bshuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.stop();
myMediaPlayer.release();
position = (int) (mySongs.size()* Math.random())+1;
play(position);
}
});
}
void play(int p)
{
Uri u = Uri.parse(mySongs.get(p).toString());
myMediaPlayer = MediaPlayer.create(getApplicationContext(),u);
String curName = mySongNameList.get(p);
String[] aray = new String[curName.length()];
aray = m.WordsUsingSplit(curName);
label.setText(aray[0]);
myMediaPlayer.start();
myMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
bnext.performClick();
}
});
}
}