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"

@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;
}

}

有帮助吗?

解决方案

Retrieve the duration from the onPrepared callback...this will ensure the music(audio) is properly loaded before you attempt to get it's duration.

song.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer song) {
        int duration = song.getDuration();
        song.start();
        controller.show();
    }
});

StopSong():

private void StopSong() {
    if (song != null) {
        song.stop();
        song.release();
   }
}

for the duration text field, i will used global variable to access from all functions.

@Override
protected void onCreate(Bundle savedInstanceState) {
    int totalDuration 0;
    .
    .
    .
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
             mHandler.removeCallbacks(mUpdateTimeTask);
             totalDuration = song.getDuration(); // here
    .
    .
    .

        private void StopSong() {
            if (song!=null) {
                song.stop();
                song.release();
                totalDuration = 0; // here
        }

i hope this helps!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top