SeekBar no actualiza Android

Tengo un SeekBar en mi artículo de ListView . Y una canción para cada artículo. Cuando la canción se reproduce quiero SeekBar para actualizar o controlar MediaPlayer progres con SeekBar . Hasta ahora lo he hecho sólo para controlar, arrastrando el pulgar para fijar el progreso de MediaPlayer . El problema es que no puedo conseguirlo para actualizar la posición de SeekBar Thumb.

Este es mi código:

  Ids holder; private final Utils utils = new Utils(); private final Handler handler = new Handler(); long totalDuration; long currentPosition; 

 holder.seekbar.setMax(100); holder.seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { handler.removeCallbacks(updateTimeProgressBar); int fullDuration = Item.mp.getDuration(); int currentPosition = utils.progressToTimer( seekBar.getProgress(), fullDuration); Item.mp.seekTo(currentPosition); updateProgressBar(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { handler.removeCallbacks(updateTimeProgressBar); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); holder.seekbar.setFocusable(true); holder.seekbar.setEnabled(true); holder.seekbar.setTag(position); 

reproducir archivo

 Item.mp = MediaPlayer.create(context, Integer.valueOf(this.songPos[songPosInt]).intValue()); Item.mp.start(); holder.seekbar.setProgress(0); updateProgressBar(); 

y finalmente código para actualizar SeekBar

 public void updateProgressBar() { handler.postDelayed(updateTimeProgressBar, 1000); } private final Runnable updateTimeProgressBar = new Runnable() { @Override public void run() { if (Item.isPlaying == true && pausedSamePos == false) { currentPosition = Item.mp.getCurrentPosition(); totalDuration = mp.getDuration(); int currentTime = (utils.getProgressPercentage(totalDuration, currentPosition)); holder.seekbar.setProgress(currentTime); Log.i("CALLED", "SHOULD BE UPDATED " + currentPosition + "of total" + totalDuration); handler.postDelayed(this, 1000); } } }; 

Utils es una clase de ayuda simple para convertir MS a SEC También he intentado con

 int totalDuration=mp.getDuration() /1000; holder.seekbar.setMax(totalduration); .. int currentPosition=mp.getCurrentPosition()/1000; holder.seekbar.setProgress(currentPosition); 

Barra de busqueda

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_frame3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <SeekBar android:id="@+id/seekbar" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_weight="1.0" android:paddingLeft="5.0dip" android:paddingRight="5.0dip" android:progressDrawable="@drawable/seekbar_line" android:thumb="@drawable/seekbar_thumb" /> <TextView android:id="@+id/textview_duration_sb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10.0dip" android:text="00:05" android:textAppearance="?android:textAppearanceSmall" android:textColor="#8C000000" /> </LinearLayout> 

Y en mi vista ListView

  <FrameLayout android:id="@+id/main_frame2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5.0dip" > <TextView android:id="@+id/textview_duration" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:focusable="false" android:text="00:05" android:textColor="#8C000000" android:textSize="14.0sp" /> <include layout="@layout/play_item" /> </FrameLayout> 

Esta es mi implementación y está funcionando para mí:

En algún lugar de tu código agrega esto (una vez por canción):

 _seekBar.setProgress(0); _seekBar.setMax(_mediaPlayer.getDuration() / 1000); 

Y luego actualice el buscador usando este código:

 private Runnable _updateRunnable = new Runnable() { public void run() { if (_hasStopped) { return; } updateSeekbarView(); } }; private synchronized void updateSeekbarView() { if (_hasStopped) { reset(); return; } if (_mediaPlayer == null) { return; } if (!_mediaPlayer.isPlaying()) { stop(); return; } long totalDuration = _mediaPlayer.getDuration(); long currentDuration = _mediaPlayer.getCurrentPosition(); _txtMax.setText("" + AudioUtils.milliSecondsToTimer(totalDuration)); _txtCurrent.setText("" + AudioUtils.milliSecondsToTimer(currentDuration)); _seekBar.setProgress((int) (currentDuration / 1000)); _handler.postDelayed(_updateRunnable, 1000); } 

y la implementación de AudioUtils.milliSecondsToTimer() en caso de que quiera mostrar el temporizador como texto junto a su barra de búsqueda:

 public static String milliSecondsToTimer(long milliseconds) { String finalTimerString = ""; String secondsString = ""; // Convert total duration into time int hours = (int) (milliseconds / (1000 * 60 * 60)); int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60); int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000); // Add hours if there if (hours > 0) { finalTimerString = hours + ":"; } // Prepending 0 to seconds if it is one digit if (seconds < 10) { secondsString = "0" + seconds; } else { secondsString = "" + seconds; } finalTimerString = finalTimerString + minutes + ":" + secondsString; // return timer string return finalTimerString; } 

El problema era que el SeekBar incorrecto estaba actualizando. Tan apenas agrega el oyente de SeekBar y clickListener dentro de clickListener y trabajará. También extienda el adaptador base y obtenga el ID del artículo.

En este caso, creo que la referencia titular se descarga o cambia a otra vista.

Puede solucionar esto inicializando el Runnable en el método getView así:

 View getView(){ ... updateTimeProgressBar = new Runnable() { Ids holderCopy = holder; @Override public void run() { if (Item.isPlaying == true && pausedSamePos == false) { currentPosition = Item.mp.getCurrentPosition(); totalDuration = mp.getDuration(); int currentTime = (utils.getProgressPercentage(totalDuration, currentPosition)); holderCopy.seekbar.setProgress(currentTime); Log.i("CALLED", "SHOULD BE UPDATED " + currentPosition + "of total" + totalDuration); handler.postDelayed(this, 1000); } } }; } 

Pero realmente necesito saber cómo usted juega su canción para poder …

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.