Monday, March 20, 2017

android media player black screen

            mMediaPlayer = new MediaPlayer(this, false);
            mMediaPlayer.setDataSource(filePath);
            mSurfaceHolder.setKeepScreenOn(true);
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnSeekCompleteListener(this);
            mMediaPlayer.setOnErrorListener(this);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);


The only working tricky workaround for this problem:

Description: Post a delayed Runnable to check onPrepared status, if failed, reinitialize the engine and remove the callback in case of onPrepared get called
The very important note is that, onPause is called after the failure so do not remove callbacks there and instead use surfaceDestroyed method of SurfaceHolder.Callback interface




 private void OpenMedia(String filePath, long lastPosition) {
        doCleanUp();

        handler.postDelayed(onPreparedErrorInvestigatorRunnable, 5000);

         Log.d(TAG, "Starting MediaPlayer initialization");
         mMediaPlayer = new MediaPlayer(this, false);
           ...
 }

    @Override
    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");

        // no investigation required
        handler.removeCallbacks(onPreparedErrorInvestigatorRunnable);

       ...
    }

   @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        Log.d(TAG, "PlayerSurface destroyed");
        handler.removeCallbacks(onPreparedErrorInvestigatorRunnable);
    }

    Runnable onPreparedErrorInvestigatorRunnable = new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "Vitamio Initialization Error Detected, Retrying ...");
            releaseMediaPlayer();
            OpenMedia(lastPlaybackInfo.videoFilePath, lastPlaybackInfo.lastPosition);
        }
    };

No comments:

Post a Comment