Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextureView playing video turns black after onPause

I am using a TextureView to play a video in a ListView. The TextureView itself works perfectly, however, if I press the home button and reenter the application a few times, the TextureView turns black (though the audio continues to play). If I exit and reenter again, the TextureView turns white (or maybe transparent, as white is the colour of my background).

Here is my code:

holder.instagramTextureView
    .setSurfaceTextureListener(new SurfaceTextureListener() {

        @Override
        public void onSurfaceTextureUpdated(
                SurfaceTexture surface) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSurfaceTextureSizeChanged(
                SurfaceTexture surface, int width, int height) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onSurfaceTextureDestroyed(
                SurfaceTexture surface) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onSurfaceTextureAvailable(
                SurfaceTexture surface, int width, int height) {
            final Surface s = new Surface(surface);

            try {
                holder.mMediaPlayer = new MediaPlayer();
                holder.mMediaPlayer.setDataSource(post
                                .getMedias().get(0)
                                .getMediaUrlVideomp4StandardRes());
                holder.mMediaPlayer.setSurface(s);
                holder.mMediaPlayer.prepare();

                holder.instagramVideoVolume = 0f;
                holder.mMediaPlayer.setVolume(
                        holder.instagramVideoVolume,
                        holder.instagramVideoVolume);
                //holder.mMediaPlayer.setOnBufferingUpdateListener(this);

                holder.mMediaPlayer
                    .setOnCompletionListener(new OnCompletionListener() {
                        @Override
                        public void onCompletion(
                                MediaPlayer mp) {

                            // To play:
                            mp.reset();
                            try {
                                mp.setDataSource(post
                                    .getMedias()
                                    .get(0)
                                    .getMediaUrlVideomp4StandardRes());
                                mp.prepare();

                            } catch (IllegalArgumentException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            } catch (SecurityException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            } catch (IllegalStateException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            }
                            // <--- Here comes a call to
                            // "To Resize" which is shown
                            // right above this code
                            mp.start();
                        }
                });

                // holder.mMediaPlayer.setOnPreparedListener(this);
                //holder.mMediaPlayer.setOnVideoSizeChangedListener(this);

                holder.mMediaPlayer
                    .setAudioStreamType(AudioManager.STREAM_MUSIC);
                holder.mMediaPlayer.start();

                holder.instagramTextureView
                    .setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (holder.instagramVideoVolume == 1) {
                                holder.instagramVideoVolume = 0f;
                                holder.mMediaPlayer
                                    .setVolume(
                                        holder.instagramVideoVolume,
                                        holder.instagramVideoVolume);
                            } else {
                                holder.instagramVideoVolume = 1f;
                                holder.mMediaPlayer
                                    .setVolume(
                                        holder.instagramVideoVolume,
                                        holder.instagramVideoVolume);
                                    }
                            }
                });

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
});
like image 333
Userrrrrrrrr Avatar asked Jul 21 '15 15:07

Userrrrrrrrr


1 Answers

Problem was making a new mediaplayer each time the surface is available. Not quite sure how this worked, but this was the problem. Instead, I create the mediaplayer outside of this listener.

like image 170
Userrrrrrrrr Avatar answered Nov 09 '22 04:11

Userrrrrrrrr