eneim / toro

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

java.lang.IllegalStateException: Player is playing while it is not in managed state #416

Open tranquoctrungcntt opened 5 years ago

tranquoctrungcntt commented 5 years ago

i am using recyclerview, when i scroll list , this thing happen

java.lang.IllegalStateException: Player is playing while it is not in managed state: ViewHolder{f71e59c position=8 id=-1, oldPos=-1, pLpos:-1}
        at im.ene.toro.widget.Container.onChildDetachedFromWindow(Container.java:259)
        at android.support.v7.widget.RecyclerView.dispatchChildDetached(RecyclerView.java:7254)
        at android.support.v7.widget.RecyclerView$5.removeViewAt(RecyclerView.java:867)
        at android.support.v7.widget.ChildHelper.removeViewAt(ChildHelper.java:168)
        at android.support.v7.widget.RecyclerView$LayoutManager.removeViewAt(RecyclerView.java:8374)
        at android.support.v7.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:8647)
        at android.support.v7.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1369)
        at android.support.v7.widget.LinearLayoutManager.recycleViewsFromStart(LinearLayoutManager.java:1415)
        at android.support.v7.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1484)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1508)
        at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1331)
        at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1075)
        at android.support.v7.widget.RecyclerView.scrollStep(RecyclerView.java:1832)
        at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1927)
        at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:3187)
        at android.view.View.dispatchTouchEvent(View.java:11843)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2975)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2654)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:555)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1894)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3508)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:508)
        at android.view.View.dispatchPointerEvent(View.java:12082)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5463)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5247)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4737)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4790)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4756)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4887)
2019-02-03 20:33:20.879 16902-16902/com.example.tranquoctrungcntt.uchat E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4764)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4944)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4737)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4790)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4756)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4764)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4737)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7363)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7337)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7295)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7514)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:193)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:182)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7445)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7550)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1041)
        at android.view.Choreographer.doCallbacks(Choreographer.java:847)
        at android.view.Choreographer.doFrame(Choreographer.java:768)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1027)
        at android.os.Handler.handleCallback(Handler.java:809)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7555)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
jaytarub commented 5 years ago

Also experiencing this issue.. were you able to fix this issue @tranquoctrungcntt ?

tranquoctrungcntt commented 5 years ago

not yet, hope @eneim helps us

eneim commented 5 years ago

@tranquoctrungcntt @jaytarub Can you guys explain more about this issue? When it happens (scroll slowly or fast scrolling, Android version? Device model, library version?).

tranquoctrungcntt commented 5 years ago

@eneim I used the lasted version of library, and when I scroll ( just scroll, even slow or fast ) , my app will crash . thanks for helping @eneim

eneim commented 5 years ago

@tranquoctrungcntt Can you send me the code of your ViewHolder that implement ToroPlayer? It will be even better if you can extract your code to a small project that can still reproduce the issue.

tranquoctrungcntt commented 5 years ago

sorry but now i am doing a new approach. i will do it again as soon as posible

eneim commented 5 years ago

@tranquoctrungcntt Thanks and sorry for the inconvenience.

tranquoctrungcntt commented 5 years ago

btw, i have 2 difference layouts to inflat and i did exactly what you wrote in README.

SteveOye commented 4 years ago

@eneim Am getting thesame error today

Below is my code

`public class ListAdapter extends RecyclerView.Adapter { @Nullable ExoPlayerViewHelper helper;

Uri mediaUri;
Context context;
ArrayList<Item> item = new ArrayList<>();

public ListAdapter(ArrayList<Item> item) {
    this.item = item;
}

@Override
public int getItemViewType(int position) {
    if (item.get(position).getType().equalsIgnoreCase("image")) {
        return 0;
    } else {
        return 1;
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view;

    if (viewType == 0) {
        view = inflater.inflate(R.layout.list_two, parent, false);
        return new listOneVM(view);
    }
    view = inflater.inflate(R.layout.list_one, parent, false);
    return new listTwoVM(view);
}

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

    if (item.get(position).getType().equalsIgnoreCase("image")) {
        //set the view holder for ads with video
        listOneVM listOneVM = (listOneVM) holder;
        listOneVM.textView.setText(item.get(position).getName());
        listOneVM.bind(Uri.parse(item.get(position).getUrl()));

    } else {
        listTwoVM listTwoVM = (listTwoVM) holder;
        listTwoVM.textView.setText(item.get(position).getName());
        listTwoVM.textView2.setText(item.get(position).getAge());
    }
}

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

class listOneVM extends RecyclerView.ViewHolder implements ToroPlayer {

    TextView textView;

    PlayerView playerView;

    public listOneVM(@NonNull View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.name);
        playerView = itemView.findViewById(R.id.video_player);
    }

    // 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();
    }

    @Override
    public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) {
        if (helper == null) {
            helper = new ExoPlayerViewHelper(this, mediaUri);
            helper.addPlayerEventListener(toroPlayerEventListener);
        }
        helper.initialize(container, playbackInfo);
    }

    private ToroPlayer.EventListener toroPlayerEventListener = new EventListener() {
        @Override
        public void onFirstFrameRendered() {
        }

        @Override
        public void onBuffering() {
        }

        @Override
        public void onPlaying() {

        }

        @Override
        public void onPaused() {

        }

        @Override
        public void onCompleted() {

        }
    };

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

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

    @Override
    public boolean isPlaying() {
        boolean isPlaying = false;
        try {
            isPlaying = helper != null && helper.isPlaying();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isPlaying;
    }

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

    @Override
    public boolean wantsToPlay() {
        try {
            return ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= 0.50;
        } catch (Exception e) {
            e.printStackTrace();
            release();
            return false;
        }
    }

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

`

Here is the error

Process: com.example.torotest, PID: 21028 java.lang.IllegalStateException: Player is playing while it is not in managed state: listOneVM{e23054a position=1 id=-1, oldPos=-1, pLpos:-1} at im.ene.toro.widget.Container.onChildDetachedFromWindow(Container.java:259) at androidx.recyclerview.widget.RecyclerView.dispatchChildDetached(RecyclerView.java:7539) at androidx.recyclerview.widget.RecyclerView$5.removeViewAt(RecyclerView.java:872) at androidx.recyclerview.widget.ChildHelper.removeViewAt(ChildHelper.java:168) at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeViewAt(RecyclerView.java:8639) at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:8912) at androidx.recyclerview.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1429) at androidx.recyclerview.widget.LinearLayoutManager.recycleViewsFromStart(LinearLayoutManager.java:1479) at androidx.recyclerview.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1554) at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1578) at androidx.recyclerview.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1391) at androidx.recyclerview.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1128) at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:1841) at androidx.recyclerview.widget.RecyclerView.scrollByInternal(RecyclerView.java:1940) at androidx.recyclerview.widget.RecyclerView.onTouchEvent(RecyclerView.java:3391) at android.view.View.dispatchTouchEvent(View.java:12527) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3026) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2705) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3032) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3032) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3032) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3032) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3032) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3032) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:444) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1835) at android.app.Activity.dispatchTouchEvent(Activity.java:3465) at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69) at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:402) at android.view.View.dispatchPointerEvent(View.java:12768) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5287) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5087) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4602) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4655) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4621) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4761) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4629) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4818) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4602) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4655) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4621) 2020-07-19 21:23:49.763 21028-21028/com.example.torotest E/AndroidRuntime: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4629) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4602) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7330) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7299) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7260) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7436) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:247) at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:212) at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7404) at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7459) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092) at android.view.Choreographer.doCallbacks(Choreographer.java:888) at android.view.Choreographer.doFrame(Choreographer.java:813) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6810) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)