关闭活动时如何从Recyclerview停止Exoplayer?

时间:2019-09-10 15:17:11

标签: android android-recyclerview exoplayer

在我的应用中,有一个带有exoplayer的recyclerview。当我离开应用程序或更改屏幕时,视频将继续播放。离开屏幕时,我需要使其暂停。

我尝试使用onViewRecycled方法,但是仅当我返回到正在播放视频的屏幕时,视频才暂停。

@Override
public void onViewRecycled(@NonNull MyViewHolder holder) {
    super.onViewRecycled(holder);
    if (holder.simpleExoPlayer != null) {
        holder.positionExo = holder.simpleExoPlayer.getCurrentPosition();
        holder.simpleExoPlayer.setPlayWhenReady(false);
    }
}

我的适配器:

    @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
                   //...
    holder.simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(context);

                    // Bind the player to the view.
                    holder.videoCnt.setPlayer(holder.simpleExoPlayer);

                    holder.videoUri = Uri.parse(content.getVideo());

                    // Produces DataSource instances through which media data is loaded.
                    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
                            Util.getUserAgent(context, context.getString( R.string.app_name)));
                    // This is the MediaSource representing the media to be played.
                    MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
                            .createMediaSource(holder.videoUri);
                    // Prepare the player with the source.
                    holder.simpleExoPlayer.prepare(videoSource);

                   //...
}

如何正确暂停视频?

0 个答案:

没有答案
相关问题