# Spinnende Seekbar



## Lucaaa (23. Jan 2019)

Hallo ich habe eine Seekbar um in einer Mp3 Datei zu einer bestimmten Stelle zu springen. 
Das Problem ist, dass wenn ich den Fortschritt über die Seekbar ändere, die Anzeige erst wieder zurück zum alten Wert springt, und dann wieder nach vorn.
(Die Seekbar wird regelmäßig von einem Service geupdatet)

Die Seekbar

```
seekPlaying.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(fromUser) {
            tvProgress.setText(lib.getDurationFormattedd(progress*1000));

        }
         }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    isSeekBarDragging=true;
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        isSeekBarDragging=false;
        tvProgress.setText(lib.getDurationFormattedd(seekBar.getProgress()));
        Intent mediaService =new Intent(PlayerActivity.this, MediaPlayerService.class);
        mediaService.putExtra("seek", seekBar.getProgress()*1000);
        mediaService.setAction(MediaPlayerService.ACTION.ACTION_SEEK);
        startService(mediaService);

    }
});
```

Receiver für die neuen Daten

```
private BroadcastReceiver playerStatusReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle statusData = intent.getBundleExtra("statusdata");
        if(statusData.getBoolean(MediaPlayerService.STATUS.STATUS_PLAYING)) {
            btnPlay.setBackgroundResource(R.drawable.ic_pause_circle_filled_black_24dp);
        } else {
            btnPlay.setBackgroundResource(R.drawable.ic_play_circle_filled_black_24dp);
        }
          tvDuration.setText(lib.getDurationFormattedd(statusData.getInt(MediaPlayerService.STATUS.STATUS_CHAPTERDURATION)));
        seekPlaying.setMax(statusData.getInt(MediaPlayerService.STATUS.STATUS_CHAPTERDURATION)/1000);


       if(!isSeekBarDragging) {
           seekPlaying.setProgress(statusData.getInt(MediaPlayerService.STATUS.STATUS_PROGRESS)/1000);
           tvProgress.setText(lib.getDurationFormattedd(statusData.getInt(MediaPlayerService.STATUS.STATUS_PROGRESS)));
       }

       // book title and chapter
        if(statusData.getString(MediaPlayerService.STATUS.STATUS_TITLE)!=null &&
                statusData.getString(MediaPlayerService.STATUS.STATUS_CHAPTERNAME)!=null) {
            tvChapter.setText(statusData.getString(MediaPlayerService.STATUS.STATUS_CHAPTERNAME));
            tvBook.setText(statusData.getString(MediaPlayerService.STATUS.STATUS_TITLE));
        }

        if(statusData.getBoolean(MediaPlayerService.STATUS.STATUS_ALIVE)) {
           isMediaServiceAlive = true;
        } else {
           isMediaServiceAlive = false;
        }

        isPlaying = statusData.getBoolean(MediaPlayerService.STATUS.STATUS_PLAYING);
    }
};
```
Gesendet werden die Daten wie gesagt regelmäßig

Die Methode zum aktuallisieren des Service

```
…
case ACTION.ACTION_SEEK :
    if(mediaPlayer!=null)
        book.setCurrentChapterprogress(intent.getIntExtra("seek", 1));
    if(mediaPlayer.isPlaying()) {
        updateMediaPlayer();
         play();
    } else {
        updateMediaPlayer();
    }
     break;
...
```

vielleicht noch die updateMedialayer Methode

```
private void updateMediaPlayer() {
    try {
       mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.setDataSource(this,
                Uri.fromFile(new File(chapters[book.getCurrentChapter()-1].getPath())));
        mediaPlayer.prepare();
        mediaPlayer.seekTo((int)book.getCurrentChapterprogress());
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    showNotification();
}
```


----------

