title: Play/Pause Media Player in Texture View Surface Texture Listener
body: <p>I've implemented a recycler view and each item will play video.  My problem is that I need to pause/play the video when it scrolls on/off the screen.</p>
  
  <p>I've implemented that when the user scrolls the video keeps playing.  </p>
  
  <p>I have had an issue trying to keep track of the media player when the view scrolls off screen.</p>
  
  <p>I have come up with a solution and I'm hoping I can get some advice to see if may be any issues with the solution.</p>
  
  <p>So I'm using a texture view to display the media player in the recycler view as videoviews aren't compatible when looking to use a recycler or customise the controller.</p>
  
  <p>So in the surface texture destroyed (signalling that the view has left the screen) I pause the media player and when the surfacetexture is available (signalling the view is on screen) I check if the media player is null (as it will be when the screen first loads) if not I start the media player.  Below is my video player class in where you can see my implementation.</p>
  
  <pre><code>public class CustomVideoPlayer implements TextureView.SurfaceTextureListener, VideoControllerView.MediaPlayerControl, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener {
  
      private Context mContext;
      private MediaPlayer mMediaPlayer;
      private SurfaceTexture mSurface;
      private VideoControllerView mControllerView;
      private TextureView mTextureView;
      private ProgressBar mProgress;
      private FrameLayout mView;
  
      public CustomVideoPlayer(Context ctx, TextureView view, ProgressBar progressDialog, FrameLayout holderView){
  
          this.mContext = ctx;
          mTextureView = view;
          mTextureView.setSurfaceTextureListener(this);
          mProgress = progressDialog;
          mControllerView = new VideoControllerView(ctx);
          mView = holderView;
          mTextureView.setOnTouchListener(new ControlTouchListener());
      }
  
  
      @Override
      public boolean canPause() {
          return true;
      }
  
      @Override
      public boolean canSeekBackward() {
          return true;
      }
  
      @Override
      public boolean canSeekForward() {
          return true;
      }
  
      @Override
      public int getBufferPercentage() {
          return 0;
      }
  
      @Override
      public int getCurrentPosition() {
          return mMediaPlayer.getCurrentPosition();
      }
  
      @Override
      public int getDuration() {
          return mMediaPlayer.getDuration();
      }
  
      @Override
      public boolean isPlaying() {
          return mMediaPlayer.isPlaying();
      }
  
      @Override
      public void pause() {
          mMediaPlayer.pause();
      }
  
      @Override
      public void seekTo(int i) {
          mMediaPlayer.seekTo(i);
      }
  
      @Override
      public void start() {
          mMediaPlayer.start();
      }
  
      @Override
      public boolean isFullScreen() {
          return false;
      }
  
      @Override
      public void toggleFullScreen() {
  
      }
  
      @Override
      public void onBufferingUpdate(MediaPlayer mp, int percent) {
  
      }
  
      @Override
      public void onCompletion(MediaPlayer mp) {
  
      }
  
  
  
      @Override
      public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
  
      }
  
      @Override
      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
          mSurface = surface;
          if(mMediaPlayer!=null) {
              mMediaPlayer.setSurface(new Surface(mSurface));
              mMediaPlayer.start();
          }
          Log.i(VersysVideoPlayer.class.getSimpleName(), String.valueOf(surface)+"available");
      }
  
      @Override
      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
  
      }
  
      @Override
      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
          //mSurface = surface;
          if(mMediaPlayer==null){
              Log.i(VersysVideoPlayer.class.getSimpleName(), "MEDIA PLAYER IS NULL");
          }else{
              Log.i(CustomVideoPlayer.class.getSimpleName(), "MEDIA PLAYER IS NOT NULL");
              mMediaPlayer.pause();
          }
          Log.i(CustomVideoPlayer.class.getSimpleName(), String.valueOf(surface)+"destroyed");
          return false;
      }
  
      @Override
      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
  
      }
  
      public void changePlayState(){
          if(mMediaPlayer!=null) {
              if (mMediaPlayer.isPlaying()) {
                  mMediaPlayer.pause();
              } else {
                  mMediaPlayer.start();
              }
          }else{
              Log.i(CustomVideoPlayer.class.getSimpleName(), "MEDIA PLAYER IS NULL");
          }
      }
  
      public void startVideo(String url){
          if(mMediaPlayer!=null){
              mMediaPlayer.reset();
              mMediaPlayer.release();
              //mMediaPlayer = new MediaPlayer();
          }else{
              mMediaPlayer = new MediaPlayer();
          }
          if(!mMediaPlayer.isPlaying()){
              try {
                  mMediaPlayer.setSurface(new Surface(mSurface));
                  mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  mMediaPlayer.setDataSource(url);
                  mMediaPlayer.prepareAsync();
                  mMediaPlayer.setOnCompletionListener(this);
                  mMediaPlayer.setOnBufferingUpdateListener(this);
                  mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
                  mMediaPlayer.setOnPreparedListener(this);
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
  
      @Override
      public void onPrepared(MediaPlayer mp) {
          Log.i(CustomVideoPlayer.class.getSimpleName(), "ON PREPARED CALLED");
          mControllerView.setMediaPlayer(this);
          mControllerView.setAnchorView(mView);
          mControllerView.show();
          mProgress.setVisibility(View.GONE);
          mMediaPlayer.start();
         // mMediaPlayer.setVolume(0,0);
      }
  
      //Touch listener to display video controls
      class ControlTouchListener implements View.OnTouchListener{
  
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              if(event.getAction() == MotionEvent.ACTION_DOWN){
                  mControllerView.show();
              }
              return false;
          }
      }
  }
  </code></pre>
  
  <p>Any comments or opinions are welcomed.</p>