Black screen when returning to video playback activity in Android

前端 未结 7 1316
星月不相逢
星月不相逢 2021-02-04 07:13

I\'m currently developing the android application ServeStream and I\'ve encountered and problem that I can\'t fix. My application will stream music and video using the android M

7条回答
  •  野性不改
    2021-02-04 07:52

    After lot of googling around and head-scratching over state diagram of MediaPlayer, I finally managed to avoid this so called black screen. I've already posted in the OP's comments section that this problem seems to be resolved with latest Android versions (greater than 4.x)and so I was opting to have similar behavior on Gingerbread devices as well.

    Needless to say, SurfaceHolder callback methods play a very crucial role in the lifecycle of a bounded MediaPlayer-SurfaceView implementation. And those very callback methods came handy to me for getting out of this situation.

    First:

    inside onCreate(): - Basic initialization stuff..

        mVideoSurface = (SurfaceView) findViewById(R.id.videoSurface);
        videoHolder = mVideoSurface.getHolder();
        videoHolder.addCallback(this);
    
        // For Android 2.2
    
        videoHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
        // Media Player and Controller
    
        player = new MediaPlayer();
        controller = new VideoControllerView(this);
    

    Then, SurfaceView Callbacks :

    don't forget to set the Display to your MediaPlayer and IMHO, surfaceCreated() is the best place to do that.

    @Override
     public void surfaceCreated(SurfaceHolder holder) {
        player.setDisplay(holder);
        try {
        player.prepareAsync();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
    

    And here's the most important thing. When user leaves the current activity by either pressing Home button or by opening a different activity with expecting any results, our SurfaceView is going to to be get destroyed. What I wanted to achieve was to resume the playback of ongoing video from the position it was playing when the context got switched. So, in order to do that,

    Save the current playback position in a variable so that we can use it later on to seek our playback to that particular position. And one more. Release the damn MediaPlayer instance. I tried to avoid releasing the MediaPlayer instance and it's re-creation but I keep failing over and over. So..point made.

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (player != null) {
            mCurrentVideoPosition = player.getCurrentPosition();
            player.release();
            player = null;
        }
    
    }
    

    Now, onPrepared() Initialize any MediaController here if you're interested. Check if the video was already playing and so seek the video to that position.

         @Override
     public void onPrepared(MediaPlayer mp) {
        controller.setMediaPlayer(this);
        controller.setAnchorView((FrameLayout) findViewById(R.id.videoSurfaceContainer));
        player.start();
    
        if (mCurrentVideoPosition != 0) {
            player.seekTo(mCurrentVideoPosition);
            player.start();
        }
    
    }
    

    Finally, to play the video:

     void playVideo(){  
    
       try {
            if (player == null) 
                player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource("SOME_VIDEO_FILE.3gp");
            player.setOnPreparedListener(this);
            player.setOnErrorListener(new OnErrorListener() {
    
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mp.reset();
                    return false;
                }
            });
    
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
         }
    

    And that's all of it. I know the issue is not there with newer versions and all great but... lots of GBs are still out there.

提交回复
热议问题