On screen rotate only video should be displayed

1.1k views Asked by At

I have used exoPlayer Library what i am trying to do is i pass data from recyclerview to next activity that works fine video is been played and title as well as desc is been fetched but when i rotate the phone i only want simpleexovideoview to displayed and video is playing but the activity name is still there. I have used < android:configChanges="orientation|screenSize" > that handles the orientation change following is snapshot of activity

Portrait view

Portrait Landscape view

Rotated Image

and code is as follows

videoplayer.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kaushal.myapplication.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">

    <android.support.constraint.Guideline
        android:id="@+id/horizontalHalf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="256dp" />


    <TextView
        android:id="@+id/VideoTitle"
        android:textSize="22sp"
        android:text="video title"
        android:textStyle="bold"
        android:layout_margin="12dp"
        android:textColor="#016699"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/horizontalHalf" />


    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/videoplayer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        app:layout_constraintBottom_toTopOf="@+id/horizontalHalf"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/VideoDesc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Video Desc"
        app:layout_constraintLeft_toLeftOf="parent"
        android:textSize="18sp"
        android:layout_margin="12dp"
        app:layout_constraintTop_toBottomOf="@+id/VideoTitle"
        tools:layout_editor_absoluteY="477dp"
        android:layout_marginLeft="12dp" />

</android.support.constraint.ConstraintLayout>

videoActivity

    package com.example.kaushal.myapplication;
        /**
         * Created by kaushal on 06-09-2017.
         */
        public class videoplay extends AppCompatActivity implements 
        ExoPlayer.EventListener {
        TextView vidtitle, videodesc;
        String videpath;
        SimpleExoPlayer exoplayer;
        SimpleExoPlayerView exoPlayerView;
        PlaybackStateCompat.Builder videosessionBuilder;
        final static String TAG = videoplay.class.getName();
        private RelativeLayout.LayoutParams paramsNotFullscreen;
        RelativeLayout rl;

        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.videoplayer);

            vidtitle = (TextView) findViewById(R.id.VideoTitle);
            videodesc = (TextView) findViewById(R.id.VideoDesc);
            exoPlayerView = (SimpleExoPlayerView) 
            findViewById(R.id.videoplayer);
            vidtitle.setText(getIntent().getStringExtra("videotitle"));
            videodesc.setText(getIntent().getStringExtra("videodesc"));
            videpath = getIntent().getStringExtra("videourl");
            mediaSession();
            Uri uri = Uri.parse(videpath);
            intializePlayer(uri);
        }
            @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

                paramsnotfullscreen= (RelativeLayout.LayoutParams)exoPlayerView.getLayoutParams();
                RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(paramsnotfullscreen);
                params.setMargins(0, 0, 0, 0);
                params.height= ViewGroup.LayoutParams.MATCH_PARENT;
                params.width=ViewGroup.LayoutParams.MATCH_PARENT;
                params.addRule(RelativeLayout.CENTER_IN_PARENT);
                exoPlayerView.setLayoutParams(params);
            }else if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
                exoPlayerView.setLayoutParams(paramsnotfullscreen);
            }
        }   //refrence = https://stackoverflow.com/questions/13011891/make-a-fullscreen-in-only-layout-land-android-when-play-videoview 

        public void intializePlayer(Uri uri) {
            DefaultTrackSelector dfs = new DefaultTrackSelector();
            DefaultLoadControl dfc = new DefaultLoadControl();
            exoplayer = ExoPlayerFactory.newSimpleInstance(this, dfs, dfc);
            exoPlayerView.setPlayer(exoplayer);
            //Prepare Media source
            String useragent = Util.getUserAgent(this, "MyApplication");
            MediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(this, useragent),
                    new DefaultExtractorsFactory(), null, null);
            exoplayer.prepare(mediaSource);
            exoplayer.setPlayWhenReady(true);
        }
        public void releasePlayer() {
            exoplayer.stop();
            exoplayer.release();
            exoplayer = null;
        }
        public void mediaSession() {
            MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(this, TAG);
          mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

            mediaSessionCompat.setMediaButtonReceiver(null);

            videosessionBuilder = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE);

            mediaSessionCompat.setPlaybackState(videosessionBuilder.build());

            mediaSessionCompat.setCallback(new mediaSessionCallback());

            mediaSessionCompat.setActive(true);
        }


        public class mediaSessionCallback extends MediaSessionCompat.Callback {


            @Override
            public void onPlay() {
                exoplayer.setPlayWhenReady(true);
            }

            @Override
            public void onPause() {
                exoplayer.setPlayWhenReady(false);
            }

            @Override
            public void onSkipToPrevious() {
                exoplayer.seekTo(0);
            }
        }


        //Exo player methods
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest) {

        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

        }

        @Override
        public void onLoadingChanged(boolean isLoading) {

        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            if ((playbackState == exoplayer.STATE_READY) && playWhenReady) {
                Log.d(TAG, "Player running");
            } else if (playbackState == exoplayer.STATE_READY) {
                Log.d(TAG, "paused");
            }
        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {

        }

        @Override
        public void onPositionDiscontinuity() {

        }

        //When Activity is been destroyed
        @Override
        protected void onDestroy() {
            super.onDestroy();
            releasePlayer();
        }
    } 
1

There are 1 answers

6
Eric On

Here is an option called same-name-layout-land.xml layout that you can handle your landscape situation and Android will take it and inflate automatically when your device rotated, with this you can manage how your Activity should be shown to the user, as long as this cool option exist, you just have to put your VideoPlayer xml tag with "match_parent" for height and width in landscape version of your xml layout.

UPDATE: Of course, if you want to your video player to take whole of screen, you have to delete the default margin of it, and for making toolbar disappear you have to create two different styles.xml. Put one into res/values-port and the other into res/values-land, and in the landscape version you have to choose a *.NoActionBar version of your themes for it.