android-youtube-player
android-youtube-player copied to clipboard
Even after calling the enableBackgroundPlayback(true) method, it still cannot play in the background.
Problem
Summary
There is a horizontally scrolling ViewPager2, where each item is a Fragment. The first Fragment has loaded a YouTubePlayer and has already started playing. After sliding to the subsequent Fragments, the playback stops.
What I've tried
the first Fragment demo code
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = (ViewGroup) inflater.inflate(R.layout.fragment_view_pager_2, container, false);
Button button = view.findViewById(R.id.add);
button.setOnClickListener(v -> {
YouTubePlayerView youTubePlayerView = new YouTubePlayerView(button.getContext().getApplicationContext());
youTubePlayerView.enableBackgroundPlayback(true);
youTubePlayerView.initialize(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
super.onReady(youTubePlayer);
player = youTubePlayer;
youTubePlayer.loadVideo(videoId, 0);
}
}, false, IFramePlayerOptions.Companion.getDefault());
view.addView(youTubePlayerView);
});
return view;
}