2017-10-17 61 views
0

你好,我希望順利地更新每一秒更新seekbar,所以我創建了一個使用定時器的方法,每秒更新seekbar,但它再次更新歌曲nd再次每秒我有搜查無處不在,但沒有得到解決方案,我不希望使用任何處理程序,我想用這種方法Seekbar一遍又一遍地更新整首歌

private void updateseekbar() { 


    seekBar.setMax(mediaPlayer.getDuration()); 

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

        seekBar.setProgress(mediaPlayer.getCurrentPosition()); 

     } 
    },1000,1000); 

這裏來解決我有implemente當歌啓動方法

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); 

} 








} 
+0

可能[SeekBar和m edia播放器在Android](https://stackoverflow.com/questions/17168215/seekbar-and-media-player-in-android) –

+0

我已經更新了問題的完整代碼 –

+0

沒有它的不重複 –

回答

1

將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(); 
    } 
} 

更新搜索欄,同時播放這首歌曲這樣

updateseekbar(true); 

停止搜索條更新這樣

updateseekbar(false); 
+0

我不想使用處理程序這次我的任務是使用計時器,它的計時器工作正常,但是當我把media.seekto(i)進行中改變seekbar它更新音樂喜歡循環 –

+0

當你使用media.seekto(i)你需要停止定時器從更新dialog.because getCurrentPosition()將不同。 – Anonymous

+0

如何停止計時器進行中的更改? –

相關問題