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

Posted

技术标签:

【中文标题】Exoplayer 音频播放背景,甚至视频已关闭【英文标题】:Exoplayer audio playing background even video was closed 【发布时间】:2019-03-26 10:42:38 【问题描述】:

下面是我的片段,其中包含 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】:

解决方案:

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

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

希望这会有所帮助。

【讨论】:

在有 exoplayer 的片段中 你需要把它放在你想关闭播放器的地方,比如releaseplayer()方法或者closePlayer()@Siva 我在一个活动中有一个可拖动的面板,在一个片段中有一个 Exoplayer。我将 Exoplayer 片段设置为可拖动面板中的顶部片段。当我滑动左侧或右侧面板关闭但没有音频时 我的问题是我需要在哪里做这些事情才能完全关闭播放器 是的,即使我在我的项目中使用它也是如此。将上述解决方案放入 - closePlayer()releasePlayer()

以上是关于Exoplayer 音频播放背景,甚至视频已关闭的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 exoplayer 一起播放单独的音频和视频?

ExoPlayer 无法播放 Adob​​e 实时流编码器流式传输的音频/视频 (RTMP)

使用 ExoPlayer 缓冲音频直播

防止 AVPlayer 取消背景音频

防止无音频 AVPlayer 暂停背景音频

空间背景视频怎么搞自动播放