-
-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sample app] Bug in Viewpager example #1144
Comments
I am able to reproduce, i think it's just a matter of calling the method at the right time. Feel free to send a pull request if you have a fix :) |
i am working on a fix but it seems i cant get it right, i am hoping for your help on this bug :) |
Hello, is there someone who can help me on this issue (Java) |
I also face the same issue, hope someone can help fix it |
I looked at the sample code and the reason this happens is probably because sometimes
public class ViewPagerFragment extends Fragment {
private final String videoId = VideoIdsProvider.getNextVideoId();
private boolean isMenuVisible;
private YouTubePlayer initializedYouTubePlayer;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_view_pager, container, false);
YouTubePlayerView youTubePlayerView = view.findViewById(R.id.youtube_player_view);
getLifecycle().addObserver(youTubePlayerView);
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
if (!isMenuVisible) {
youTubePlayer.cueVideo(videoId, 0);
} else {
youTubePlayer.loadVideo(videoId, 0);
}
initializedYouTubePlayer = youTubePlayer;
}
});
return view;
}
// pause when fragment goes offscreen
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
isMenuVisible = visible;
if (initializedYouTubePlayer != null) {
if (!isMenuVisible) {
initializedYouTubePlayer.pause();
} else {
initializedYouTubePlayer.play();
}
}
}
} You can also use viewpager2 instead of viewpager. It supports recyclerview adapter and is better than regular old viewpager. |
This is perfect! Is there an implementation using viewpager, i am making similar to tiktok but while the video is playing and going to another fragment is audio still plays in the background, does the fix above makes my current onpause, onresume redundant? |
@fdcgica If you are using And if you are trying to make an app similar to tiktok that has vertical scrolling. I recommend you to use ViewPager2 instead of regular Viewpager. Because ViewPager2 natively supports vertical scrolling and you can even use recyclerview adapter as viewapger 2 adapter. |
There is a bug in your viewpager example which is also existing in my current project, i used loadvideo in your viewpager example and every time i swipe fast to the next page the previous video plays its audio in the background.
The text was updated successfully, but these errors were encountered: