Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

android - Using the YouTube API within a fragment

I using the new YouTube API in my project. Using the example of the documentation, it works correctly:

public class Test extends  YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    String urlVideo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        urlVideo="5lbVNYAeFiQ";

        YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        youTubeView.initialize("MY_API_KEY", this);

    }

    public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
        Toast.makeText(this, "Error ", Toast.LENGTH_LONG).show();  
    }

    public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,boolean wasRestored) {
        if (!wasRestored) {
            arg1.loadVideo(urlVideo);
        }

    }
}

But in my project, I'm using a main activity that contains a ViewPager The ViewPager shows fragments, the problem is when I want to show YouTubePlayer within one fragment of ViewPager. Normally, an activity that uses youtube, extending YouTubeBaseActivity.

extends YouTubeBaseActivity implements ... { 

My question is, how I can show YouTube Player in an activity that extends from fragments?

I searched, but can not find information about my problem.

I sincerely appreciate the help.

Best regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use YouTubePlayerSupportFragment if you are only playing a YouTube video in a single support.v4.app.Fragment. This allows you to use FragmentActivity, rather than YouTubeBaseActivity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...