TextureView playing video turns black after onPause
Asked Answered
B

3

7

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();
            }
        }
});
Boxhaul answered 21/7, 2015 at 15:18 Comment(3)
dont use do e.printStackTrace(); on android projects, instead use Log.e("UNIQUE_TAG", "error doing ....", e); so you can get helpful error information via logcatJassy
Is onSurfaceTextureAvailable being called each time? If you're starting the media player each time, where are you stopping it?Nealy
Wish your comment was an answer fadden, the answer was indeed in onSurfaceTextureAvailableBoxhaul
B
2

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.

Boxhaul answered 21/7, 2015 at 20:32 Comment(0)
L
0

From my research, textureView.getSurfaceTexture() returns null onResume(). What I did was reattach a listener to it in onResume().

TextureView textureView;

/*....
Usual stuff
*/

public void onPause(){
    super.onPause();

    //... destroy or disable image producer
}

public void onResume(){
    super.onResume();

    textureView.setSurfaceTextureListener(/*another listener*/);
}

Then, perform your usual things on onSurfaceTextureAvailable()

Luben answered 14/9, 2016 at 6:55 Comment(0)
C
0

The black screen and audio playing happened with me when I did not release the MediaPlayer object.

Cartierbresson answered 22/11, 2018 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.