Skip to content
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

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

Open
huanglongyu opened this issue Jul 18, 2024 · 2 comments
Labels

Comments

@huanglongyu
Copy link

huanglongyu commented Jul 18, 2024

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
Copy link
Owner

Hi, can you share a video of the issue?

@huanglongyu
Copy link
Author

@PierfrancescoSoffritti

test.mp4

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants