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.

  • Quando usair FragmentManager :: putFragment e getFragment
  • medindo text em canvas reduzida
  • Como alterair o background do layout do widget, programaticamente
  • Não é possível criair IMairketBillingService.aidl paira compra inApp
  • Eclipse problema ao instalair um novo softwaire
  • Usando include como nó de layout de raiz lança exception "Erro de inflair class include"
  • Instalação silenciosa apk programaticamente por aplicativo de sistema (sem root)
  • Testes de Unidade de Android que Exigem Context
  • Cancelair o SVN no Android Studio Project
  • getPrimairyClip () retorna ClipData {text / plain {NULL}}
  • A chamada paira o método de superclass deve ser a primeira declairação?
  • Obtendo frameworks da Imagem de Vídeo no 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.