在屏幕上,只显示视频

时间:2017-09-11 11:38:08

标签: java android youtube android-orientation exoplayer2.x

我使用exoPlayer库我想要做的是我将数据从recyclerview传递到下一个活动工作精细视频已经播放和标题以及desc被取出但是当我旋转手机我只想简单的视频浏览到显示和视频正在播放但活动名称仍然存在。 我用过< android:configChanges =“orientation | screenSize”>处理方向改变的是活动的快照

纵向视图

Portrait 横向视图

Rotated Image

和代码如下

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 个答案:

答案 0 :(得分:0)

这是一个名为 same-name-layout -land.xml布局的选项,您可以处理您的景观情况,Android会在您的设备旋转时自动接收并充气,您可以管理如何向用户显示您的活动,只要存在这个酷选项,您只需将您的VideoPlayer xml标记与&#34; match_parent&#34;对于xml布局的横向版本中的高度和宽度。

更新: 当然,如果你想让你的视频播放器占据整个屏幕,你必须删除它的默认边距,并且为了使工具栏消失,你必须创建两个不同的styles.xml。将一个放入res / values-port,另一个放入res / values-land,在横向版本中,你必须为它选择一个* .NoActionBar版本的主题。