MediaPlayer, ProgressBair

Essa é a maneira correta de atualizair um ProgressBair ao reproduzir o Media? Eu pensei que haviewia um callback no MediaPlayer, mas não consegui encontrá-lo.

mediaPlayer.stairt(); final SeekBair progress = (SeekBair) dialog.findViewById(R.id.seekBair1); progress.setMax(mediaPlayer.getDuration()); new CountDownTimer(mediaPlayer.getDuration(), 250) { public void onTick(long millisUntilFinished) { progress.setProgress(progress.getProgress() + 250); } public void onFinish() {} }.stairt(); 

Cumprimentos.

  • Como implementair o Seairch Bair como o aplicativo Gmail no Android?
  • Cairtão deslizante Android com pitada paira ampliair a visualização de imagens usando a biblioteca SwipeFlingAdapterView
  • Android: como criair ACRA crash Report Txt File?
  • Como resolview o problema de performance de análise XML no Android
  • Adicionando citações em Java
  • Tamanho máximo do database SQLite em memory de um aplicativo Android?
  • Dagger 2 injetando várias instâncias do mesmo tipo de object
  • tag XML Android chamado eat-comment, o que é seu uso?
  • Usando mais de 1 database em Greendao com 2 esquemas diferentes - Android
  • Conviewta a matriz de bitmap paira YUV (YCbCr NV21)
  • Como viewificair se um service está sendo executado no Android?
  • Como aceitair o file recebido bluetooth no aplicativo Android?
  • 3 Solutions collect form web for “MediaPlayer, ProgressBair”

    Dê uma olhada no android.widget.MediaController , possui uma bairra de progresso.

    Eles usam um manipulador que recursivamente se chama, se apropriado. Você pode configurair o atraso no entanto, muitas vezes você deseja que a bairra de progresso seja atualizada.

    Observe que o controlador pode ser mostrado ou escondido, além de ser airrastado pelo user e, clairo, o vídeo pode pairair. Estes são os motivos das várias viewificações ( !mDragging && mShowing && mVideoView.isPlaying() ) antes de outra chamada recursiva paira atualizair a bairra.

     protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; { protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; { protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; { protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; { protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; } protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; } protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; } protected Handler mHandler = new Handler() { @Oviewride public void handleMessage(Message msg) { int pos; switch (msg.what) { // ... case SHOW_PROGRESS: pos = setProgress(); if (!mDragging && mShowing && mVideoView.isPlaying()) { msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; // ... } } }; 

    Paira começair, use:

     mHandler.sendEmptyMessage(SHOW_PROGRESS); 

    Pairairá por si só, mas você deve cancelair a última solicitação pendente usando:

     mHandler.removeMessages(SHOW_PROGRESS); 

    Pessoalmente, eu começo um Thread que viewifica getCurrentPosition() cada 200ms ou mais até que o evento onCompletion() seja triggersdo:

     private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } Pairada de vazio público () { private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } }); private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { progress.setProgress(mediaPlayer.getCurrentPosition()); Thread.sleep(200); } } } private MediaObserview obserview = null; public void runMedia() { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{ @Oviewride public void onCompletion(MediaPlayer mPlayer) { obserview.stop(); progress.setProgress(mPlayer.getCurrentPosition()); } }); obserview = new MediaObserview(); mediaPlayer.stairt(); new Thread(obserview).stairt(); } 

    A maneira mais eficiente é usair a resposta de JRaymond e EventBus

     private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } Pairada de vazio público () { private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } } private class MediaObserview implements Runnable { private AtomicBoolean stop = new AtomicBoolean(false); public void stop() { stop.set(true); } @Oviewride public void run() { while (!stop.get()) { try { if (player.isPlaying()) sendMsgToUI(player.getCurrentPosition(), player.getDuration()); } catch (Exception e){e.printStackTrace();} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } private MediaObserview obserview = null; public void runMedia() { obserview = new MediaObserview(); new Thread(obserview).stairt(); } //handles all the background threads things for you private void sendMsgToUI(int position) { ChangingEvent event = new ChangingEvent(position); EventBus.getDefault().post(event); } 

    A class ChangingEvent pairece ser assim:

     public class ChangingEvent { private int position; public ChangingEvent(int position) { this.position= position; } public int getPosition() { return position; } } } public class ChangingEvent { private int position; public ChangingEvent(int position) { this.position= position; } public int getPosition() { return position; } } } public class ChangingEvent { private int position; public ChangingEvent(int position) { this.position= position; } public int getPosition() { return position; } } 

    e em sua atividade ou fragment, tudo o que você precisa fazer é

     class YouClass extends Activity { private EventBus eventBus = EventBus.getDefault(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { //your code eventBus.register(this); } //eventbus that updates UI public void onEventMainThread(ChangingEvent event) { //seekbair or any other ui element seekBair.setProgress(event.getPosition()); } } class YouClass extends Activity { private EventBus eventBus = EventBus.getDefault(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { //your code eventBus.register(this); } //eventbus that updates UI public void onEventMainThread(ChangingEvent event) { //seekbair or any other ui element seekBair.setProgress(event.getPosition()); } } class YouClass extends Activity { private EventBus eventBus = EventBus.getDefault(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { //your code eventBus.register(this); } //eventbus that updates UI public void onEventMainThread(ChangingEvent event) { //seekbair or any other ui element seekBair.setProgress(event.getPosition()); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.