Seekbar 一次又一次地更新整首歌曲

Seekbar Updating whole song again and again

你好,我想平滑地更新搜索栏,它每秒更新一次,所以我创建了一个使用计时器的方法,它每秒更新一次搜索栏,但它每秒一次又一次地更新歌曲,我到处搜索但没有获取解决方案我不想使用我想使用此方法修复的任何处理程序

 private  void updateseekbar() {


    seekBar.setMax(mediaPlayer.getDuration());

     new Timer().schedule(new TimerTask() {
       @Override
       public void run() {

                   seekBar.setProgress(mediaPlayer.getCurrentPosition());

       }
   },1000,1000);

这里我实现了歌曲开始时的方法

  public void play(View v) {

    if (mediaPlayer == null) {
        mediaPlayer = MediaPlayer.create(this, songlist.get(index));
        mediaPlayer.start();
        updateseekbar();
        imageview.setImageResource(R.drawable.img_btn_play_pressed);
    } else if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
        imageview.setImageResource(R.drawable.img_btn_pause);
    } else if (mediaPlayer != null) {
        mediaPlayer.start();
        imageview.setImageResource(R.drawable.img_btn_play_pressed);



    }

这是我的完整代码

private SeekBar seekBar;
TextView startduration,totalduration;
ImageView imageview, forwardd, backword, next, previous;
ArrayList<Integer> songlist = new ArrayList<>();
MediaPlayer mediaPlayer;
int seektime = 5000;
int index = 0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    imageview = (ImageView) findViewById(R.id.imageview);
    forwardd = (ImageView) findViewById(R.id.forwardd);
    backword = (ImageView) findViewById(R.id.backword);
    next = (ImageView) findViewById(R.id.next);
    previous = (ImageView) findViewById(R.id.previous);
    startduration= (TextView) findViewById(R.id.startduration);
    totalduration= (TextView) findViewById(R.id.totalduration);


    songlist.add(R.raw.diljaniya);
    songlist.add(R.raw.stand);
    songlist.add(R.raw.haledil);
    songlist.add(R.raw.myname);
    songlist.add(R.raw.fiftyways);
    songlist.add(R.raw.desiboys);

    previous = (ImageView) findViewById(R.id.previous);

 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
     @Override
     public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

         seekBar.setMax(mediaPlayer.getDuration());
         mediaPlayer.seekTo(i);


     }

     @Override
     public void onStartTrackingTouch(SeekBar seekBar) {

     }

     @Override
     public void onStopTrackingTouch(SeekBar seekBar) {

     }
 });

    forwardd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (mediaPlayer != null) {
                int med = mediaPlayer.getCurrentPosition();
                if (med <= mediaPlayer.getDuration()) {
                    mediaPlayer.seekTo(med + seektime);
                    seekBar.setProgress(med+seektime);


                }
            } else {

            }
        }
    });
    backword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (mediaPlayer != null) {
                int med = mediaPlayer.getCurrentPosition();
                if (med - seektime >= 0) {
                    mediaPlayer.seekTo(med - seektime);
                    seekBar.setProgress(med-seektime);
                } else {
                }
            } else {
            }
        }
    });
}

public void play(View v) {

    if (mediaPlayer == null) {
        mediaPlayer = MediaPlayer.create(this, songlist.get(index));
        mediaPlayer.start();
        updateseekbar();
        imageview.setImageResource(R.drawable.img_btn_play_pressed);
    } else if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
        imageview.setImageResource(R.drawable.img_btn_pause);
    } else if (mediaPlayer != null) {
        mediaPlayer.start();
        imageview.setImageResource(R.drawable.img_btn_play_pressed);



    }

}

public void previouss(View v) {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.reset();
    }

    if (index < songlist.size() - 1) {
        mediaPlayer = MediaPlayer.create(this, songlist.get(index));
        mediaPlayer.start();
        seekBar.setProgress(0);
    } else {
        index = 0;
        return;
    }
    index++;
}

public void next(View v) {

    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.reset();
    }
    if (index < songlist.size()) {
        mediaPlayer = MediaPlayer.create(this, songlist.get(index));
        mediaPlayer.start();
        seekBar.setProgress(0);
    } else {
        index = 0;
        return;
    }
    index++;
}


private  void updateseekbar() {


    seekBar.setMax(mediaPlayer.getDuration());

     new Timer().schedule(new TimerTask() {
       @Override
       public void run() {

                   seekBar.setProgress(mediaPlayer.getCurrentPosition());

       }
   },1000,1000);

}








}

像这样声明Timer为全局成员变量;

Timer timer=new Timer();

像这样更新 updateseekbar 方法中的歌曲

  private void updateseekbar(Boolean songPlay) {
    if(songPlay){
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                seekBar.setProgress(mediaPlayer.getCurrentPosition());

            }
        },1000,1000);
        handler.postDelayed(runnable,0);
    }else {
        timer.cancel();
        timer.purge();
    }
}

像这样播放歌曲时更新 seekBar

updateseekbar(true);

像这样停止seekBar更新

updateseekbar(false);