onResume()ExoPlayer视频显示黑屏

时间:2020-04-21 02:05:30

标签: java android

在onResume()上,我的ExoPlayer视频屏幕为黑色。 LogCat显示,退出应用程序并返回时,包含视频播放器的SurfaceView被销毁,然后重新创建。我不确定是什么问题,您是否对如何解决此问题或检查代码有什么建议?

/*
        // new for resuming a video
        if(videoPlayer != null) {
            videoPlayer.setPlayWhenReady(playWhenReady);
            videoPlayer.seekTo(currentWindow, playbackPosition);
            videoPlayer.prepare(videoSource, false, false);
        }
*/

        this.context = context.getApplicationContext();
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        videoSurfaceDefaultHeight = point.x;
        screenDefaultHeight = point.y;

        //videoSurfaceView.setVisibility(View.INVISIBLE);
        videoSurfaceView = new PlayerView(this.context);
        //videoSurfaceView.setControllerHideOnTouch(true);
        videoSurfaceView.hideController();
        videoSurfaceView.setVisibility(View.INVISIBLE);
        videoSurfaceView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
        //videoSurfaceView.setUseController(true);


        if(videoSurfaceView != null) {
            SurfaceView v = (SurfaceView) videoSurfaceView.getVideoSurfaceView();
            surfaceHolder = v.getHolder();
            surfaceHolder.addCallback(this);
        }


        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory =
                new AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector =
                new DefaultTrackSelector(videoTrackSelectionFactory);

        // 2. Create the player
        videoPlayer = ExoPlayerFactory.newSimpleInstance(context , trackSelector);
        // Bind the player to the view.
        //videoSurfaceView.setUseController(true);

        videoSurfaceView.setPlayer(videoPlayer);
        setVolumeControl(VolumeState.ON);

        // Attach player to the view.
        playerControlView.setPlayer(videoPlayer);

        // Prepare the player with the dash media source.
        //videoPlayer.prepare(createMediaSource());


        //https://stackoverflow.com/questions/52365953/exoplayer-playerview-onclicklistener-not-working
        //https://codelabs.developers.google.com/codelabs/exoplayer-intro/#5
        //https://exoplayer.dev/ui-components.html
        videoSurfaceView.setOnClickListener(view -> {
        //videoPlayer.getVideoSurfaceView().setOnClickListener(view -> {
            Log.e("GET", "clicked!");
            if(videoPlayer.getPlayWhenReady()){
                videoPlayer.setPlayWhenReady(false);
            }else{
                videoPlayer.setPlayWhenReady(true);
            }
        });

        playVideo();


        videoPlayer.addListener(new Player.EventListener() {
            @Override
            public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {

            }

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

            }

            @Override
            public void onLoadingChanged(boolean isLoading) {

            }

            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                switch (playbackState) {

                    case Player.STATE_BUFFERING:
                        Log.e(TAG, "onPlayerStateChanged: Buffering video.");
                        if (progressBar != null) {
                            progressBar.setVisibility(View.VISIBLE);
                            videoSurfaceView.setUseController(false);
                        }

                        break;
                    case Player.STATE_ENDED:
                        Log.d(TAG, "onPlayerStateChanged: Video ended.");
                        videoPlayer.seekTo(0);
                        break;
                    case Player.STATE_IDLE:

                        break;
                    case Player.STATE_READY:
                        Log.e(TAG, "onPlayerStateChanged: Ready to play.");
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }
                        if(!isVideoViewAdded){
                            addVideoView();
                        }
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onRepeatModeChanged(int repeatMode) {

            }

            @Override
            public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

            }


            @Override
            public void onPositionDiscontinuity(int reason) {

            }

            @Override
            public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

            }

            @Override
            public void onSeekProcessed() {

            }


            @Override
            public void onPlayerError(ExoPlaybackException error) {
                switch (error.type) {
                    case ExoPlaybackException.TYPE_SOURCE:
                        Log.e(TAG, "TYPE_SOURCE: " + error.getSourceException().getMessage());
                        break;
                    case ExoPlaybackException.TYPE_RENDERER:
                        Log.e(TAG, "TYPE_RENDERER: " + error.getRendererException().getMessage());
                        break;
                    case ExoPlaybackException.TYPE_UNEXPECTED:
                        Log.e(TAG, "TYPE_UNEXPECTED: " + error.getUnexpectedException().getMessage());
                        break;
                }
            }

        });
    }

0 个答案:

没有答案