android-youtube-player icon indicating copy to clipboard operation
android-youtube-player copied to clipboard

Even after calling the enableBackgroundPlayback(true) method, it still cannot play in the background.

Open huanglongyu opened this issue 7 months ago • 2 comments

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;
    }

huanglongyu avatar Jul 18 '24 10:07 huanglongyu