0

The following code allows you to start a video player and play the video.

The problem is that the NavigationBar hides a piece of the video, I would like to make it transparent.

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.widget.MediaController;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.MotionEvent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.VideoView;

public class SimpleVideoStream extends Activity implements
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {
    private String TAG = getClass().getSimpleName();
    private VideoView mVideoView = null;
    private MediaPlayer mMediaPlayer = null;
    private MediaController mMediaController = null;
    private ProgressBar mProgressBar = null;
    private String mVideoUrl;
    private Boolean mShouldAutoClose = true;
    private boolean mControls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Bundle b = getIntent().getExtras();
        mVideoUrl = b.getString("mediaUrl");
        mShouldAutoClose = b.getBoolean("shouldAutoClose", true);
        mControls = b.getBoolean("controls", true);

        RelativeLayout relLayout = new RelativeLayout(this);
        relLayout.setBackgroundColor(Color.BLACK);
        RelativeLayout.LayoutParams relLayoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relLayoutParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        mVideoView = new VideoView(this);
        mVideoView.setLayoutParams(relLayoutParam);
        relLayout.addView(mVideoView);

        // Create progress throbber
        mProgressBar = new ProgressBar(this);
        mProgressBar.setIndeterminate(true);
        // Center the progress bar
        RelativeLayout.LayoutParams pblp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        pblp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        mProgressBar.setLayoutParams(pblp);
        // Add progress throbber to view
        relLayout.addView(mProgressBar);
        mProgressBar.bringToFront();

        setOrientation(b.getString("orientation"));

        setContentView(relLayout, relLayoutParam);

        play();
    }

    private void play() {
        mProgressBar.setVisibility(View.VISIBLE);
        Uri videoUri = Uri.parse(mVideoUrl);
        try {
            mVideoView.setOnCompletionListener(this);
            mVideoView.setOnPreparedListener(this);
            mVideoView.setOnErrorListener(this);
            mVideoView.setVideoURI(videoUri);
            mMediaController = new MediaController(this);
            mMediaController.setAnchorView(mVideoView);
            mMediaController.setMediaPlayer(mVideoView);
            if (!mControls) {
                mMediaController.setVisibility(View.GONE);
            }
            mVideoView.setMediaController(mMediaController);
        } catch (Throwable t) {
            Log.d(TAG, t.toString());
        }
    }

    private void setOrientation(String orientation) {
        if ("landscape".equals(orientation)) {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }else if("portrait".equals(orientation)) {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    private Runnable checkIfPlaying = new Runnable() {
        @Override
        public void run() {
            if (mVideoView.getCurrentPosition() > 0) {
                // Video is not at the very beginning anymore.
                // Hide the progress bar.
                mProgressBar.setVisibility(View.GONE);
            } else {
                // Video is still at the very beginning.
                // Check again after a small amount of time.
                mVideoView.postDelayed(checkIfPlaying, 100);
            }
        }
    };

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "Stream is prepared");
        mMediaPlayer = mp;
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mVideoView.requestFocus();
        mVideoView.start();
        mVideoView.postDelayed(checkIfPlaying, 0);
    }

    private void pause() {
        Log.d(TAG, "Pausing video.");
        mVideoView.pause();
    }

    private void stop() {
        Log.d(TAG, "Stopping video.");
        mVideoView.stopPlayback();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy triggered.");
        stop();
    }

    private void wrapItUp(int resultCode, String message) {
        Log.d(TAG, "wrapItUp was triggered.");
        Intent intent = new Intent();
        intent.putExtra("message", message);
        setResult(resultCode, intent);
        finish();
    }

    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "onCompletion triggered.");
        stop();
        if (mShouldAutoClose) {
            wrapItUp(RESULT_OK, null);
        }
    }

    public boolean onError(MediaPlayer mp, int what, int extra) {
        StringBuilder sb = new StringBuilder();
        sb.append("MediaPlayer Error: ");
        switch (what) {
            case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
            sb.append("Not Valid for Progressive Playback");
            break;
            case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
            sb.append("Server Died");
            break;
            case MediaPlayer.MEDIA_ERROR_UNKNOWN:
            sb.append("Unknown");
            break;
            default:
            sb.append(" Non standard (");
            sb.append(what);
            sb.append(")");
        }
        sb.append(" (" + what + ") ");
        sb.append(extra);
        Log.e(TAG, sb.toString());

        wrapItUp(RESULT_CANCELED, sb.toString());
        return true;
    }

    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        Log.d(TAG, "onBufferingUpdate : " + percent + "%");
    }

    @Override
    public void onBackPressed() {
        // If we're leaving, let's finish the activity
        wrapItUp(RESULT_OK, null);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // The screen size changed or the orientation changed... don't restart the activity
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mMediaController != null)
            mMediaController.show();
        return false;
    }
}

I tried to add this, but it does not work very well, the problem is that the progressBar overlaps the NavigationBar, when the device is in portrait mode.

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mMediaController != null){
            mMediaController.show();
            this.getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
              | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
              | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
              | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
              | View.SYSTEM_UI_FLAG_IMMERSIVE
              );
        }
        return false;
    }

Paul
  • 3,644
  • 9
  • 47
  • 113
  • can you show some screenshot of the screen when the video is playing and how nav bar is hiding content? – Rai_Gaurav Jul 31 '18 at 10:37
  • @Rai_Gaurav: Tell me how much I use, onTouchEvent? – Paul Jul 31 '18 at 10:48
  • I need to see it first, how navigation bar is hiding your content, so if you post some visuals like screen shot of your videoView, it will be helpfull – Rai_Gaurav Jul 31 '18 at 10:51
  • @Rai_Gaurav: Ok, I do not know if you understand well, but in the first is black, in the second there is transparency using onTouchEvent. With its related problem. – Paul Jul 31 '18 at 10:58
  • Ok now I understood that you want to change the color of the mediaControl of videoView to transparent – Rai_Gaurav Jul 31 '18 at 11:01
  • actually, its default, if you see other videoPlayer they also have show same way but if you want to change it, you have to make Custom MediaController for VideoView. You can also try this stack overflow answer over here [Transparency of MediaController](https://stackoverflow.com/a/49597087/5353830) – Rai_Gaurav Jul 31 '18 at 11:26

0 Answers0