在我的应用程序中,我正在尝试实现 SeekBar。第一次播放声音时,SeekBar 工作正常。但是当我单击按钮再次播放声音时,我会陷入关于MediaPlayer的无休止的错误循环。我想要的是,当我在播放声音时,如果我点击播放按钮,声音应该从头开始播放,SeekBar 也应该从头开始。如果我删除了 SeekBar,那么一切正常。但是 SeekBar 在我的应用程序中是必须的。我已经在 BaseAdapter 中实现了所有这些东西。我必须在 BaseAdapter 中做所有的事情。所以请帮助我让我使用BaseAdapter的代码..
这是我的代码...请看一下...
mediaPlayer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(activity, "Loading Message", Toast.LENGTH_LONG).show();
mp.reset();
try {
mp.setDataSource("url for any mp3 audio file");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(final MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mediaPlayer.setBackgroundResource(R.drawable.m_pause);
seekBar.setProgress(0);
seekBar.setMax(100);
mUpdateTimeTask = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
seekBar.setProgress(progress);
mHandler.post(this);
}
});
mUpdateTimeTask.start();
}
});
}
});
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.setBackgroundResource(R.drawable.m_play);
mp.stop();
try {
mUpdateTimeTask.join();
mHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mHandler.removeCallbacks(mUpdateTimeTask);
}
});
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(final SeekBar seekBar) {
// TODO Auto-generated method stub
//mHandler.removeCallbacks(mUpdateTimeTask);
if(mp.isPlaying())
{
int totalDuration = mp.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
mp.seekTo(currentPosition);
mUpdateTimeTask = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//timer.setText(""+utils.milliSecondsToTimer(currentDuration));
seekBar.setProgress(progress);
mHandler.post(this);
}
});
mUpdateTimeTask.start();
}
else
{
seekBar.setProgress(0);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if(mp.isPlaying())
{
mHandler.removeCallbacks(mUpdateTimeTask);
}
else
{
seekBar.setProgress(0);
}
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
mp.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
return false;
}
});
谢谢...