在我的音乐播放器中,单击停止按钮后如何让我的搜索栏回到起点?并将总持续时间和当前位置的文本字段设置为零??当我单击停止然后单击播放按钮时会出现问题?在我的日志猫中,错误是“尝试在没有有效媒体播放器的情况下调用 getDuration”
@Override
protected void onCreate(Bundle savedInstanceState) {
setVolumeControlStream(AudioManager.STREAM_MUSIC);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton play_btn= (ImageButton) findViewById(R.id.iv_play);
ImageButton backward_btn=(ImageButton)findViewById(R.id.imageView_backward);
ImageButton forward_btn= (ImageButton) findViewById(R.id.imageView_forward);
ImageButton stop_btn= (ImageButton) findViewById(R.id.imageView_stop);
ImageButton pause_btn= (ImageButton)findViewById(R.id.imageView_pause);
ImageButton shuffle_btn = (ImageButton) findViewById(R.id.imageView_shuffle);
song = MediaPlayer.create(getApplicationContext() ,R.raw.adele);
songProgressBar= (SeekBar) findViewById(R.id.seekBar1);
songProgressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = song.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
song.seekTo(currentPosition);
UpdateProgressBar();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
pause_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PauseSong(); }
private void PauseSong() {
// TODO Auto-generated method stub
song.pause();
}
});
stop_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StopSong();
}
private void StopSong() {
// TODO Auto-generated method stub
song.stop();
}
});
play_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PlaySong();
}
private void PlaySong() {
// TODO Auto-generated method stub
song.start();
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
UpdateProgressBar();
}
});
}
private void UpdateProgressBar() {
// TODO Auto-generated method stub
mHandler.postDelayed(mUpdateTimeTask, 100);
}
Runnable mUpdateTimeTask = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
totalDuration = song.getDuration();
currentDuration = song.getCurrentPosition();
// Displaying Total Duration time
songTotalDurationLabel = (TextView)findViewById(R.id.songTotalDurationLabel);
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel = (TextView)findViewById(R.id.songCurrentDurationLabel);
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
protected void onPause() {finish();}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}