在我的代码中,前进和后退(这些是按钮单击事件)在文件播放(停止)后工作。我的目标是在播放文件时前进/后退。
这是我的代码
forward.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// get current song position
int currentPosition = mp.getCurrentPosition();
// check if seekForward time is lesser than song duration
if(currentPosition + seekForwardTime <= mp.getDuration()){
// forward song
mp.seekTo(currentPosition + seekForwardTime);
}else{
// forward to end position
mp.seekTo(mp.getDuration());
}
}
});
backward.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// get current song position
int currentPosition = mp.getCurrentPosition();
// check if seekBackward time is greater than 0 sec
if(currentPosition - seekBackwardTime >= 0){
// forward song
mp.seekTo(currentPosition - seekBackwardTime);
}else{
// backward to starting position
mp.seekTo(0);
}
// update timer progress again
updateProgressBar();
}
});
private void playAudio(String path)
{
Log.i("play","riyasssss");
playing_started=true;
// Play song
try {
mp.reset();
mp.setDataSource(path);
mp.prepare();
mp.start();
// Changing Button Image to pause image
play.setText("Puase");
// set Progress bar values
sb.setProgress(0);
sb.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Update timer on seekbar
* */
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
// Displaying Total Duration time
total_time.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
current_time.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
sb.setProgress(progress);
if(current_time.getText().toString().equalsIgnoreCase(total_time.getText().toString()))
{
//playingStarted=0;
play.setText("Play");
}
if(totalDuration<currentDuration)
{
mHandler.postDelayed(this, 10000000);
System.out.println("time task waited !!!!!!!!!!!");
}
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
`
请给我一个解决方案..Thanx提前