Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release ExoPlayer in RecyclerView

I am implementing the ExoPlayer in a RecyclerView. But while scrolling the Video stops, but not the audio.

How can I release the ExoPlayer in the RecyclerView? Or how can I get the position of the Recycled Object so I am able to release the ExoPlayer?

This is my Data Object

public class            enter code here           Video {
    private String url;
    private Context context;
    Boolean playWhenReady = false;
    int currentWindow = 0;
    long playbackPosition = 0;
    ExoPlayer player;

    public Video(String url, Context context) {
        this.url = url;
        this.context = context;
    }

    public void setCurrentWindow(int currentWindow) {
        this.currentWindow = currentWindow;
    }

    public void setPlaybackPosition(long playbackPosition) {
        this.playbackPosition = playbackPosition;
    }

    public void setPlayWhenReady(Boolean playWhenReady) {
        this.playWhenReady = playWhenReady;
    }

    public void setPlayer(ExoPlayer player) {
        this.player = player;
    }

    public ExoPlayer getPlayer() {
        return player;
    }

    public String getUrl() {
        return url;
    }

    public Context getContext() {
        return context;
    }

    public Boolean getPlayWhenReady() {
        return playWhenReady;
    }

    public int getCurrentWindow() {
        return currentWindow;
    }

    public long getPlaybackPosition() {
        return playbackPosition;
    }
}

And this my Adapter Class: I tried it with the onViewRecycled() Method, but because I dont get the position i can´t release the player.

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {
    private ArrayList<Video> mDataset;


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public PlayerView mExoPlayer;

        public ViewHolder(View itemView) {
            super(itemView);
            mExoPlayer = itemView.findViewById(R.id.video_view);
        }
    }

    public VideoAdapter(ArrayList<Video> myDataset) {
        mDataset = myDataset;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView=LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


        initPlayer(holder,position);

    }

    private void initPlayer(ViewHolder holder,int position) {
        mDataset.get(position).setPlayer(ExoPlayerFactory.newSimpleInstance(
                new DefaultRenderersFactory(mDataset.get(position).getContext()),
                new DefaultTrackSelector(), new DefaultLoadControl()));

        holder.mExoPlayer.setPlayer(mDataset.get(position).getPlayer());

        mDataset.get(position).getPlayer().setPlayWhenReady(mDataset.get(position).getPlayWhenReady());
        mDataset.get(position).getPlayer().seekTo(mDataset.get(position).getCurrentWindow(), mDataset.get(position).getPlaybackPosition());

        Uri uri = Uri.parse(mDataset.get(position).getUrl());
        MediaSource mediaSource = buildMediaSource(uri);
        mDataset.get(position).getPlayer().prepare(mediaSource, true, false);
    }

    private MediaSource buildMediaSource(Uri uri) {

        return new ExtractorMediaSource.Factory(
                new DefaultHttpDataSourceFactory("exoplayer-codelab")).
                createMediaSource(uri);
    }


    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        super.onViewRecycled(holder);
    }


}
like image 518
Alex G Avatar asked Aug 04 '18 13:08

Alex G


1 Answers

I think that it would be cleanest to release the player when its view is recycled.

Adapter Class:

@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
    int position = holder.getAdapterPosition();
    if (mDataset.get(position) != null) {
        mDataset.get(position).getPlayer().release();
    }
    super.onViewRecycled(holder);
}
like image 88
Taylor Venissat Avatar answered Nov 02 '22 07:11

Taylor Venissat