eneim / toro

Video list auto playback made simple, specially built for RecyclerView
Apache License 2.0
1.41k stars 253 forks source link

Video not retaining the play state once scrolled back to the top of the list. #470

Open devmanishjoshi opened 4 years ago

devmanishjoshi commented 4 years ago

Video on the top of the list starts playing as expected. Scroll to 5th-6th item -> video that falls in major part of the screen, starts playing. Once you scroll list back to top and come back to same video which was playing earlier, video starts streaming from the beginning and volume is set to default. Expected result : Until Activity is recreated or the list is refreshed video should retain the state. It is possible to achieve using this library or do I have to search other work around for this problem.

[ ] Toro version : 3.7.0.2905-A1 [ ] Android version : Android 9 [ ] Samsung Galaxy S9

Below is the ViewHolder Code :

public class SimpleViewHolder extends RecyclerView.ViewHolder implements ToroPlayer, View.OnClickListener {

    private final Context context;
    @Nullable
    ExoPlayerViewHelper helper;
    @Nullable
    private Uri mediaUri;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.video_view)
    PlayerView playerView;
    @BindView(R.id.icon_volume)
    public ImageView iconVolume;
//    @BindView(R.id.icon_play)
//    ImageView iconPlay;

    SimpleViewHolder(Context context, View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);

        this.context = context;
    }

    // called from Adapter to setup the media
    void bind(Uri item) {
        if (item != null) {
            mediaUri = item;
        }
    }

    @NonNull @Override public View getPlayerView() {
        return playerView;
    }

    @NonNull @Override public PlaybackInfo getCurrentPlaybackInfo() {
        return helper != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo(0,0);
    }

    @Override
    public void initialize(@NonNull Container container, @Nullable PlaybackInfo playbackInfo) {
        if (helper == null) {
            helper = new ExoPlayerViewHelper(this, mediaUri);
            //playbackInfo.setResumePosition(100);
            //playbackInfo.setResumeWindow(100);
            playbackInfo.setVolumeInfo(new VolumeInfo(true,0));

            helper.addPlayerEventListener(new ToroPlayer.EventListener() {
                @Override
                public void onFirstFrameRendered() {

                }

                @Override
                public void onBuffering() {
                        progressBar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onPlaying() {
                    progressBar.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onPaused() {
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onCompleted() {
                }
            });
        }
        helper.initialize(container, playbackInfo);
        iconVolume.setOnClickListener(this);
    }

    @Override public void release() {
        if (helper != null) {
            helper.release();
            helper = null;
        }
    }

    @Override public void play() {
        if (helper != null) helper.play();

    }

    @Override public void pause() {
        if (helper != null) helper.pause();
    }

    @Override public boolean isPlaying() {
        return helper != null && helper.isPlaying();
    }

    @Override public boolean wantsToPlay() {
        return ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= 0.85;
    }

    @Override public int getPlayerOrder() {
        return getAdapterPosition();
    }

    @Override
    public void onClick(View v) {
        PlaybackInfo info = getCurrentPlaybackInfo();
        VolumeInfo volumeInfo = info.getVolumeInfo();
        if(volumeInfo.isMute()){
            volumeInfo.setMute(false);
            volumeInfo.setVolume(100.0f);
            info.setVolumeInfo(volumeInfo);
            iconVolume.setImageDrawable(context.getDrawable(R.drawable.ic_volume_up));
        }else{
            volumeInfo.setMute(true);
            volumeInfo.setVolume(0.0f);
            info.setVolumeInfo(volumeInfo);
            iconVolume.setImageDrawable(context.getDrawable(R.drawable.ic_volume_off));
        }
//        helper.setVolumeInfo(volumeInfo);
        helper.setPlaybackInfo(info);
    }
}

Please let me know if I need to be more specific or you need more information about the issue