1

在我的音乐播放器中,单击停止按钮后如何让我的搜索栏回到起点?并将总持续时间和当前位置的文本字段设置为零??当我单击停止然后单击播放按钮时会出现问题?在我的日志猫中,错误是“尝试在没有有效媒体播放器的情况下调用 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;
}

}

4

1 回答 1

0

从 onPrepared 回调中检索持续时间...这将确保在您尝试获取其持续时间之前正确加载音乐(音频)。

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

停止歌曲():

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

对于持续时间文本字段,我将使用全局变量来访问所有函数。

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

我希望这有帮助!

于 2013-09-02T14:51:35.900 回答