danylovolokh / VideoPlayerManager

This is a project designed to help controlling Android MediaPlayer class. It makes it easier to use MediaPlayer ListView and RecyclerView. Also it tracks the most visible item in scrolling list. When new item in the list become the most visible, this library gives an API to track it.
3.15k stars 746 forks source link

java.lang.IllegalStateException #18

Open sonnguyen1187 opened 8 years ago

sonnguyen1187 commented 8 years ago

When i'm try the demo with play videos when scroll in recycle-view android some time it crash this is the log file 02-15 14:45:13.910 23491-23530/? E/MediaPlayerWrapperImpl@1131714648: 11698 catch IO exception [java.io.IOException: Prepare failed.: status=0x1] 02-15 14:45:14.710 23491-23491/? E/MediaPlayer: Should have subtitle controller already set 02-15 14:45:15.530 23491-23491/? E/MediaPlayer: Should have subtitle controller already set 02-15 14:46:06.350 23491-23503/? E/BufferQueue: [unnamed-23491-58] dequeueBuffer: BufferQueue has been abandoned! 02-15 14:46:06.360 23491-23502/? E/MediaPlayer: error (1, -19) 02-15 14:46:06.360 23491-23530/? E/MediaPlayerWrapperImpl@1124914064: 11698 catch IO exception [java.io.IOException: Prepare failed.: status=0x1] 02-15 14:46:06.400 23491-2798/? E/AndroidRuntime: FATAL EXCEPTION: VideoPlayerView@1124209904 Process: com.volokh.danylo.videolist, PID: 23491 java.lang.IllegalStateException at android.media.MediaPlayer._setVideoSurface(Native Method) at android.media.MediaPlayer.setSurface(MediaPlayer.java:761) at com.volokh.danylo.video_player_manager.ui.MediaPlayerWrapper.setSurfaceTexture(MediaPlayerWrapper.java:496) at com.volokh.danylo.video_player_manager.ui.VideoPlayerView$5.run(VideoPlayerView.java:549) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:157) at android.os.HandlerThread.run(HandlerThread.java:61) 02-15 14:46:06.640 23491-23491/? E/MediaPlayer: Should have subtitle controller already set 02-15 14:46:07.210 23491-2851/? E/BufferQueue: [unnamed-23491-67] query: BufferQueue has been abandoned! 02-15 14:46:07.640 23491-23491/? E/MediaPlayer: Should have subtitle controller already set

danylovolokh commented 8 years ago

Please be as much specific as you can with bug reports: Device, steps to reproduce.

sonnguyen1187 commented 8 years ago

Thanks for reply I'm using samsung note 3 I'm add more item (40) in VideoRecyclerViewFragment and make fast scroll down and up this bug will happen .

Thanks

ajchili commented 8 years ago

I'm unsure if you are still facing this issue, but for anyone using recyclerviews, set this as your addOnScrollListener

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
                if(!mList.isEmpty() && mLayoutManager.findFirstVisibleItemPosition() < recyclerView.getAdapter().getItemCount() && (mLayoutManager.findLastVisibleItemPosition() + 1) < recyclerView.getAdapter().getItemCount()){
                    // need to call this method from list view handler in order to have filled list
                    mRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            mVideoVisibilityCalculator.onScrollStateIdle(
                                    mItemsPositionGetter,
                                    mLayoutManager.findFirstVisibleItemPosition(),
                                    mLayoutManager.findLastVisibleItemPosition());
                        }
                    });

                    mScrollState = scrollState;
                    if(scrollState == RecyclerView.SCROLL_STATE_IDLE){
                        mVideoVisibilityCalculator.onScrollStateIdle(
                                mItemsPositionGetter,
                                mLayoutManager.findFirstVisibleItemPosition(),
                                mLayoutManager.findLastVisibleItemPosition());
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if(!mList.isEmpty() && mItemsPositionGetter.getFirstVisiblePosition() < recyclerView.getAdapter().getItemCount() && (mLayoutManager.findLastVisibleItemPosition() + 1) < recyclerView.getAdapter().getItemCount()){
                    // need to call this method from list view handler in order to have filled list
                    mRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            mVideoVisibilityCalculator.onScrollStateIdle(
                                    mItemsPositionGetter,
                                    mLayoutManager.findFirstVisibleItemPosition(),
                                    mLayoutManager.findLastVisibleItemPosition());
                        }
                    });

                    mVideoVisibilityCalculator.onScroll(
                            mItemsPositionGetter,
                            mLayoutManager.findFirstVisibleItemPosition(),
                            mLayoutManager.findLastVisibleItemPosition() - mLayoutManager.findFirstVisibleItemPosition() + 1,
                            mScrollState);
                }
            }
        });

it will fix issues regarding array index out of bounds exceptions and stops the illegal state from occuring

ajchili commented 8 years ago

I have a new update for this issue, I was able to fix it with the code above. However there was still a crashes that occurred due to scrolling before the data was loaded and some videos would not pause on scroll. This code has fixed this for me. https://gist.github.com/ajchili/4ff1a5b077a72616cea280ba6db60118

RockyLin commented 7 years ago

@ajchili Can not open : https://gist.github.com/ajchili/4ff1a5b077a72616cea280ba6db60118

ajchili commented 7 years ago

Here is the code from the gist

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
        if(!mList.isEmpty()){
            // need to call this method from list view handler in order to have filled list
            mRecyclerView.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        mVideoVisibilityCalculator.onScrollStateIdle(
                                mItemsPositionGetter,
                                mLayoutManager.findFirstVisibleItemPosition(),
                                mLayoutManager.findLastVisibleItemPosition());
                    } catch (ArrayIndexOutOfBoundsException e) {

                    }
                }
            });

            mScrollState = scrollState;
            try {
                mVideoVisibilityCalculator.onScrollStateIdle(
                        mItemsPositionGetter,
                        mLayoutManager.findFirstVisibleItemPosition(),
                        mLayoutManager.findLastVisibleItemPosition());
            } catch (ArrayIndexOutOfBoundsException e) {

            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if(!mList.isEmpty()){
            // need to call this method from list view handler in order to have filled list
            mRecyclerView.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        mVideoVisibilityCalculator.onScrollStateIdle(
                                mItemsPositionGetter,
                                mLayoutManager.findFirstVisibleItemPosition(),
                                mLayoutManager.findLastVisibleItemPosition());
                    } catch (ArrayIndexOutOfBoundsException e) {

                    }
                }
            });

            try {
                mVideoVisibilityCalculator.onScrollStateIdle(
                        mItemsPositionGetter,
                        mLayoutManager.findFirstVisibleItemPosition(),
                        mLayoutManager.findLastVisibleItemPosition());
            } catch (ArrayIndexOutOfBoundsException e) {

            }
        }
    }
});
RockyLin commented 7 years ago

@ajchili Thanks anyway! I've already abandoned this lib, and move on to another. VideoPlayerManager is a good Lib for learning and experiment.

jeremyyu123 commented 7 years ago

@ajchili i changed the code like you suggested , but the problem still exist, outOfBoundException and illegalStateException while quick fling

ajchili commented 7 years ago

@jeremyyu123 would you be able to send me a gist of your code or allow me to fork your repository

jeremyyu123 commented 7 years ago

I found it may because of the xrecyclerview lib i use in my project , it contains a loadingmorefooter view and it causes the wrong index .Thanks for the reply

ajchili commented 7 years ago

@jeremyyu123 hopefully that's the fix, always glad to help