Exoplayer音频播放背景,甚至视频也已关闭

时间:2018-10-22 11:11:23

标签: android draggable exoplayer

下面是我的片段,其中包含exoplayer,它将随Activity中的可拖动面板一起膨胀。我的问题是在向左或向右拖动可拖动面板时,视频正在关闭,但音频仍在后台播放。如果我滑过可拖动面板,音频也需要关闭。尝试了许多来源,找不到任何解决方案。在片段和活动中,滑下可拖动面板时,onPause()方法都不会触发。同样,也不会调用public void onClosedToLeft()方法。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dragpanelone, container, false);

        try {
            exoPlayerView = (SimpleExoPlayerView)v.findViewById(R.id.exoplayer);
            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
            TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
            exoplayer = ExoPlayerFactory.newSimpleInstance(getActivity(), trackSelector);

            Uri uri = Uri.parse(videoURL);
            DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
            MediaSource mediaSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, null, null);
            exoPlayerView.setPlayer(exoplayer);
            exoplayer.prepare(mediaSource);
            exoplayer.setPlayWhenReady(true);
        } catch (Exception e){
            e.printStackTrace();
        }

        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        exoplayer.release();
    }



    @Override
    public void onStop() {
        super.onStop();

        exoplayer.setPlayWhenReady(false);

    }

    public void pausePlayback() {
        exoplayer.setPlayWhenReady(false); //pauses the playback if it's playing
    }

    private void releasePlayer() {
        if (exoplayer != null) {
            exoplayer.release();
            exoplayer.setPlayWhenReady(false);
            exoplayer = null;
        }
    }
public static void closePlayer()
{
    exoplayer.clearVideoSurface();
}
}

这是我的活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initializeDraggablepanel();
        hookDraggablePanelListners();


    }


    public void initializeDraggablepanel(){

        draggablepanel = (DraggablePanel)findViewById(R.id.draggable_panel);
        draggablepanel.setFragmentManager(getSupportFragmentManager());
        draggablepanel.setTopFragment(new DragpanelFragOne());
        draggablepanel.setBottomFragment(new DragPanelTwo());
        draggablepanel.setTopViewHeight(550);
        draggablepanel.initializeView();

    }

    private void hookDraggablePanelListners(){
        draggablepanel.setDraggableListener(new DraggableListener() {
            @Override
            public void onMaximized() {

            }

            @Override
            public void onMinimized() {

            }

            @Override
            public void onClosedToLeft() {

                DragpanelFragOne.closePlayer();
                Intent i = new Intent(MainActivity.this,HomeScreen.class);
                startActivity(i);

            }

            @Override
            public void onClosedToRight() {
                DragpanelFragOne.closePlayer();
                Intent i = new Intent(MainActivity.this,HomeScreen.class);
                startActivity(i);

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        DragpanelFragOne.closePlayer();
    }




    @Override
    public void onStop() {
        super.onStop();
        draggablepanel.closeToLeft();
    }



    @Override
    public void onBackPressed() {
        draggablepanel.isClickToMinimizeEnabled();

        Intent i = new Intent(MainActivity.this,HomeScreen.class);
        startActivity(i);
    }


}

1 个答案:

答案 0 :(得分:0)

解决方案:

关闭播放器时,您需要在stop()对象上调用方法exoplayer,如下所示:

if (exoPlayer != null) {
    exoPlayer.setPlayWhenReady(false);
    exoPlayer.stop();
    exoPlayer.seekTo(0);
}

希望这会有所帮助。