Code example for MediaController

Methods: setAnchorView, setMediaPlayer

0
   */ 
  private void createMediaController() { 
    checkState(mediaPlayer != null,
        "media player must be initialized before the media controller"); 
 
    mediaController = new MediaController(getActivity());
 
    mediaController.setAnchorView(getAudioView());
 
    MediaPlayerControl mediaPlayerControl = new MediaController.MediaPlayerControl() {
      @Override 
      public boolean canPause() { 
        return true; 
      } 
 
      @Override 
      public boolean canSeekBackward() { 
        return true; 
      } 
 
      @Override 
      public boolean canSeekForward() { 
        return true; 
      } 
 
      @Override 
      public int getBufferPercentage() { 
        return mediaPlayer != null ? (mediaPlayer.getCurrentPosition() * 100 / mediaPlayer.getDuration())
            : 0; 
      } 
 
      @Override 
      public int getCurrentPosition() { 
        return mediaPlayer != null ? mediaPlayer.getCurrentPosition() : 0;
      } 
 
      @Override 
      public int getDuration() { 
        return mediaPlayer != null ? mediaPlayer.getDuration() : 0;
      } 
 
      @Override 
      public boolean isPlaying() { 
        return mediaPlayer != null && mediaPlayer.isPlaying();
      } 
 
      @Override 
      public void pause() { 
        mediaPlayer.pause();
      } 
 
      @Override 
      public void seekTo(int pos) {
        mediaPlayer.seekTo(pos);
      } 
 
      @Override 
      public void start() { 
        mediaPlayer.start();
      } 
    }; 
 
    getAudioView().setOnTouchListener(new OnTouchListener() {
      @Override 
      public boolean onTouch(View v, MotionEvent event) {
        mediaController.show();
        return true; 
      } 
    }); 
 
    mediaController.setMediaPlayer(mediaPlayerControl);
  } 
 
  /** 
   * Creates a player using one of the mp3 files stored as raw resources in the 
   * application.