Android Surface view not showing current video frame for second video-ThrowExceptions

Exception or error:

I am using Surface view and Mediaplayer to play a list of videos. The first video will be auto-played and able to see the video and hear audio.
When I click other videos in the list only audio is audible but video surface not refreshed it is showing previous videos frame.
How can I refresh the surface view and render proper video?

 if (videoDataList.size() > 0) {


            mVideoSurface.getLayoutParams().height = 600;
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnVideoSizeChangedListener(this);

            controller = new VideoControllerView.Builder(this, this)
                    .withVideoSurfaceView(mVideoSurface)//to enable toggle display controller view
                    .canControlBrightness(true)
                    .canControlVolume(true)
                    .canSeekVideo(true)
                    .pauseIcon(R.drawable.ic_media_pause)
                    .playIcon(R.drawable.ic_media_play)
                    .shrinkIcon(R.drawable.ic_media_fullscreen_shrink)
                    .stretchIcon(R.drawable.ic_media_fullscreen_stretch)
                    .build((FrameLayout) findViewById(R.id.videoSurfaceContainer));//layout container that hold video play view

            mLoadingView.setVisibility(View.VISIBLE);

            try {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setDataSource(this, Uri.parse(videoDataList.get(0).getUrl()));
                mMediaPlayer.setOnPreparedListener(this);
                mMediaPlayer.setOnCompletionListener(this);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            mVideoSurface.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    controller.toggleControllerView();
                    return false;
                }
            });

            final VideoViewAdapter videoViewAdapter = new VideoViewAdapter(videoDataList, extendedcareServerAddress, waitingRoomPlayIconUrl, MainActivity.this);
            videoListView.setAdapter(videoViewAdapter);


            videoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                    if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                        mMediaPlayer.stop();
                        mMediaPlayer.reset();


                    }

                    mVideoSurface.invalidate();
                    mVideoSurface = (ResizeSurfaceView) singleVideoLayout.findViewById(R.id.videoSurface);


                    videoHolder = mVideoSurface.getHolder();

                    videoHolder.addCallback(MainActivity.this);
                    HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setOnVideoSizeChangedListener(MainActivity.this);

                    controller = new VideoControllerView.Builder(MainActivity.this, MainActivity.this)
                            .withVideoSurfaceView(mVideoSurface)//to enable toggle display controller view
                            .canControlBrightness(true)
                            .canControlVolume(true)
                            .canSeekVideo(true)
                            .pauseIcon(R.drawable.ic_media_pause)
                            .playIcon(R.drawable.ic_media_play)
                            .shrinkIcon(R.drawable.ic_media_fullscreen_shrink)
                            .stretchIcon(R.drawable.ic_media_fullscreen_stretch)
                            .build((FrameLayout) findViewById(R.id.videoSurfaceContainer));//layout container that hold video play view

                    mLoadingView.setVisibility(View.VISIBLE);
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                mMediaPlayer.setDataSource(MainActivity.this, Uri.parse(videoDataList.get(position).getUrl()));
                                mMediaPlayer.prepareAsync();
                                mMediaPlayer.setOnPreparedListener(MainActivity.this);
                                mMediaPlayer.setOnCompletionListener(MainActivity.this);
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (SecurityException e) {
                                e.printStackTrace();
                            } catch (IllegalStateException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    });

                    mVideoSurface.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            controller.toggleControllerView();
                            return false;
                        }
                    });
                }
            });
            }
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *