PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.41k stars 756 forks source link

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

Open huanglongyu opened 2 months ago

huanglongyu commented 2 months ago

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;
    }
PierfrancescoSoffritti commented 2 months ago

Hi, can you share a video of the issue?

huanglongyu commented 2 months ago

@PierfrancescoSoffritti

https://github.com/user-attachments/assets/86b41a29-5f1b-41ab-b2cb-7dc65070ce4a

I'm sorry for the late reply. The attachment is a recorded video

public class ViewPager2Fragment extends Fragment {
    private final String videoId = VideoIdsProvider.getNextVideoId();

    @SuppressLint("SetTextI18n")
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_view_pager_2, container, false);
        Button button = linearLayout.findViewById(R.id.add);
        button.setText("add " + videoId);
        button.setOnClickListener(v -> {
            YouTubePlayerView youTubePlayerView = new YouTubePlayerView(button.getContext().getApplicationContext());
            youTubePlayerView.enableBackgroundPlayback(true);
            youTubePlayerView.setEnableAutomaticInitialization(false);
            youTubePlayerView.initialize(new AbstractYouTubePlayerListener() {
                @Override
                public void onReady(@NonNull YouTubePlayer youTubePlayer) {
                    super.onReady(youTubePlayer);
                    youTubePlayer.loadVideo(videoId, 0);
                }
            }, false, IFramePlayerOptions.Companion.getDefault());
            linearLayout.addView(youTubePlayerView);

        });
        return linearLayout;
    }
}