Get the mediaPlayer of the videoView in Android

android developer picture android developer · Jun 25, 2012 · Viewed 13.3k times · Source

Is it possible to get a reference to the mediaPlayer instance that the videoView is using, preferably right inside its ctor? If so, how?

Since the videoView doesn't have as much listeners as the mediaPlayer, I would like to have the ability to reach the mediaPlayer for extra control and better events handling.

Answer

minhaz picture minhaz · Feb 8, 2013

you can listener from VideoView.

VideoView mVideoView=new VideoView();
mVideoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
  @Override
  public void onPrepared(MediaPlayer pMp) {
   //use a global variable to get the object
  }
});

Alternatively if you are only interested on Media Player Events you can use any of these and these are implemented on VideoView and these are basically MediaPlayer events.

void    setOnCompletionListener(MediaPlayer.OnCompletionListener l)
void    setOnErrorListener(MediaPlayer.OnErrorListener l)
void    setOnInfoListener(MediaPlayer.OnInfoListener l)
void    setOnPreparedListener(MediaPlayer.OnPreparedListener l)

All these function will give a Media Player instance also.