无法使用setOnClickListener全屏播放exoPlayer

Unable to play exoPlayer in full screen with setOnClickListener

默认情况下,视频将以纵向模式播放,点击 imageView1.setOnClickListener 后,视频应以 full_screen.xml

播放

但在我的情况下,我无法在 full_screen.xml 中播放视频,我还在 AndroidManifest 中添加了 <activity android:name=".Activities.FullScreen"></activity> 仍然没有成功。

FYI : when full_screen activity opens and returns to main_activity so why video is not playing in full_screen_activity

这里是VideoPlayerActivity.java

    public class VideoPlayerActivity extends AppCompatActivity {

    Uri videoUri;
    @BindView(R.id.playerView) PlayerView playerView;
    @BindView(R.id.floating_widget) ImageView imageView;
    @BindView(R.id.full_screen) ImageView imageView1;

    ExoPlayer exoPlayer;
    ExtractorsFactory extractorsFactory;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        ButterKnife.bind(this);
        Intent intent = getIntent();

        if (intent!=null){
            String uri_str = intent.getStringExtra("videoUri");
            videoUri = Uri.parse(uri_str);
        }

        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exoPlayer.setPlayWhenReady(false);
                exoPlayer.release();
                Intent serviceIntent2 = new Intent(VideoPlayerActivity.this,FullScreen.class);
                serviceIntent2.putExtra("videoUri",videoUri.toString());
                startActivity(serviceIntent2);
            }
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exoPlayer.setPlayWhenReady(false);
                exoPlayer.release();
                Intent serviceIntent = new Intent(VideoPlayerActivity.this,FloatingWidgetService.class);
                serviceIntent.putExtra("videoUri",videoUri.toString());
                startService(serviceIntent);
            }
        });

        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

        TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
        exoPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
        extractorsFactory = new DefaultExtractorsFactory();
        playVideo();

    }

    private void playVideo() {
        try {
            String playerInfo = Util.getUserAgent(this,"VideoPlayer");
            DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,playerInfo);

            MediaSource mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory,extractorsFactory,null,null);

            playerView.setPlayer(exoPlayer);
            exoPlayer.prepare(mediaSource);
            exoPlayer.setPlayWhenReady(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        exoPlayer.setPlayWhenReady(false);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exoPlayer.setPlayWhenReady(false);
        exoPlayer.release();
    }
}

这里FullScreen.java

package com.sanoj.jlplayer.Activities;

public class FullScreen extends AppCompatActivity {

    @BindView(R.id.fullscreenPlayer) PlayerView playerView;

    ExoPlayer exoPlayer;
    ExtractorsFactory extractorsFactory;

    Uri videoUri;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        Intent intent = getIntent();

        if (intent!=null){
            String uri_str = intent.getStringExtra("videoUri");
            videoUri = Uri.parse(uri_str);
        }

        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

        TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
        exoPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
        extractorsFactory = new DefaultExtractorsFactory();
        playVideo();
    }


    private void playVideo() {
        try {
            String playerInfo = Util.getUserAgent(this,"Fullscreen");
            DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,playerInfo);

            MediaSource mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory,extractorsFactory,null,null);

            playerView.setPlayer(exoPlayer);
            exoPlayer.prepare(mediaSource);
            exoPlayer.setPlayWhenReady(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        exoPlayer.setPlayWhenReady(false);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exoPlayer.setPlayWhenReady(false);
        exoPlayer.release();
    }
}

谁能帮我解决我犯的错误。

更新:

这里是full_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".Activities.FullScreen">


    <com.google.android.exoplayer2.ui.PlayerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fullscreenPlayer"
        app:controller_layout_id="@layout/exo_playback_control_view"
        app:use_controller="true">

    </com.google.android.exoplayer2.ui.PlayerView>

</RelativeLayout>

赏金更新 我想在我的 exoplayer 项目中添加全屏选项,上面是我添加全屏的代码,但是当我单击 onclickListner 时,它会移动到 main _activity,

我要添加什么节目full_screen_option。

希望这段代码能帮到你 我使用数据绑定而不是 findviewbyid。

 fullscreenButton.setOnClickListener(view -> {
            if (fullscreen) {
                exitFullSscreen(fullscreenButton);
            } else {
                binding.llToolbar.setVisibility(View.GONE);

                fullscreenButton.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_fullscreen_black_24dp));
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) binding.videoFullScreenPlayer.getLayoutParams();
                params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                params.height = ViewGroup.LayoutParams.MATCH_PARENT;
                params.setMargins(0, 0, 0, 0);
                binding.videoFullScreenPlayer.setLayoutParams(params);
                fullscreen = true;
            }
        });



private void exitFullSscreen(ImageView fullscreenButton) {

    fullscreenButton.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_fullscreen_black_24dp));
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    if (getSupportActionBar() != null) {
        getSupportActionBar().show();
    }
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // set video height
    setLayoutHeight(binding.videoFullScreenPlayer);

    binding.llToolbar.setVisibility(View.VISIBLE);
    fullscreen = false;


}



 private void setLayoutHeight(View layout) {

        float height = getResources().getDimension(R.dimen.video_height);
        ViewGroup.LayoutParams params = layout.getLayoutParams();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        layout.setLayoutParams(params);
    }

更新 ->>>

       <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/videoFullScreenPlayer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#A6000000"
            android:visibility="gone"
            app:auto_show="true"
            app:controller_layout_id="@layout/exo_playback_control_view_full"
            app:player_layout_id="@layout/exo_simple_player_view"
            app:repeat_toggle_modes="none"
            app:resize_mode="fit"
            app:show_timeout="5000"
            app:surface_type="texture_view"

            />

TL;DR:到目前为止,我已经找到了 2 个使 exoplayer 全屏显示的选项,如果您想查看完整的源代码,它们在我的 public github 上。

第一个解决方案

您使用 DialogFragment 来占据整个屏幕,我注意到旧手机有一些延迟,因此请务必先进行测试。基本上你所需要的就是把你的 playerView 放在 frameLayout

ActivityMain.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/main_media_frame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#000000">

        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/player_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" />

    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#757575"
        android:gravity="center"
        android:text="Nothing"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

</LinearLayout>

MainActivity

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initFullScreenDialog()
        initFullScreenButton()
        ...
    }

private fun initFullScreenDialog() {
        fullscreenDialog = object: Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
            override fun onBackPressed() {
                if(isFullscreen) closeFullscreenDialog()
                super.onBackPressed()
            }
        }
    }

    private fun initFullScreenButton(){
        exo_fullscreen_button.setOnClickListener {
            if (!isFullscreen) {
                openFullscreenDialog()
            } else {
                closeFullscreenDialog()
            }
        }
    }

    @SuppressLint("SourceLockedOrientationActivity")
    private fun openFullscreenDialog() {
        exo_fullscreen_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_fullscreen_shrink))
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        (player_view.parent as ViewGroup).removeView(player_view)
        fullscreenDialog?.addContentView(player_view, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        isFullscreen = true
        fullscreenDialog?.show()
    }

    private fun closeFullscreenDialog() {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
        (player_view.parent as ViewGroup).removeView(player_view)
        (main_media_frame as FrameLayout).addView(player_view)
        exo_fullscreen_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_fullscreen_expand))
        isFullscreen = false
        fullscreenDialog?.dismiss()
    }

第二种解决方案

使用 LayoutParams

修改布局

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:gravity="center" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#757575"
        android:gravity="center"
        android:text="Nothing"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />
</LinearLayout>

ActivityMain

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initFullScreenButton()
        ...
    }

private fun initFullScreenButton(){
        exo_fullscreen_button.setOnClickListener {
            if (!isFullscreen) {
                openFullscreen()
            } else {
                closeFullscreen()
            }
        }
    }

    @SuppressLint("SourceLockedOrientationActivity")
    private fun openFullscreen(){
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        exo_fullscreen_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_fullscreen_shrink))
        player_view.setBackgroundColor(ContextCompat.getColor(this, R.color.colorBlack))
        val params: LinearLayout.LayoutParams = player_view.layoutParams as LinearLayout.LayoutParams
        params.width = LinearLayout.LayoutParams.MATCH_PARENT
        params.height = LinearLayout.LayoutParams.MATCH_PARENT
        player_view.layoutParams = params
        supportActionBar?.hide()
        hideSystemUi()
        isFullscreen = true
    }

    private fun closeFullscreen() {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
        exo_fullscreen_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_fullscreen_expand))
        player_view.setBackgroundColor(ContextCompat.getColor(this, R.color.colorWhite))
        val params: LinearLayout.LayoutParams = player_view.layoutParams as LinearLayout.LayoutParams
        params.width = LinearLayout.LayoutParams.MATCH_PARENT
        params.height = 0
        player_view.layoutParams = params
        supportActionBar?.show()
        player_view.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
        isFullscreen = false
    }

    private fun hideSystemUi() {
        player_view?.systemUiVisibility = (
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }