Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid, song controller and seek bar in media player
    primarykey
    data
    text
    <p>in my music player how I can let my seek bar back to the starting point after I clicked stop button? and set the text field of total duration and current position to zero ?? the problem happen when I click stop and then click play button ?in my log cat the error is "Attempt to call getDuration without a valid mediaplayer"</p> <pre><code>@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; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload