在PagerAdapter中使用ViewPager的YoutubePlayer

时间:2017-07-31 11:18:38

标签: android android-layout android-fragments android-viewpager android-youtube-api

我正在Viewpager中加载Youtube视频,但如果视图寻呼机中有两个视频,则只能播放第二个视频。请帮忙 我想在视频寻呼机中加载所有视频,如果只有一个视频,则只有最后一个视频才能播放。

YoutubeVideo Loader类: -

public class YoutubePlayerControl implements YouTubePlayer.OnInitializedListener {
private Context mContext;
private String youtubeVideoId;

public YoutubePlayerControl(Context mContext) {
    this.mContext = mContext;
}

public void playVideo(YouTubePlayerView youTubePlayerView, String videoId) {
    this.youtubeVideoId = videoId;
    youTubePlayerView.initialize(Config.KEY_YOUTUBE, this);
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
    if (!b) {
        youTubePlayer.cueVideo(youtubeVideoId);
    }
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

}

}

这里是适配器类: -

public class MyViewPagerAdapter extends PagerAdapter {

private Context mContext;
private List<FeedMetaData> feedMetaDatas;
private LayoutInflater layoutInflater;
private YoutubePlayerControl youtubePlayerControl;

public MyViewPagerAdapter(Context mContext, List<FeedMetaData> feedMetaDatas) {
    this.mContext = mContext;
    this.feedMetaDatas = feedMetaDatas;
    layoutInflater = LayoutInflater.from(this.mContext);
    youtubePlayerControl = new YoutubePlayerControl(this.mContext);
}

@Override
public int getCount() {
    return feedMetaDatas.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view.equals(object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView = layoutInflater.inflate(R.layout.nojoto_series_viewpager_row, container, false);

    YouTubePlayerView youtubePlayerView = (YouTubePlayerView) itemView.findViewById(R.id.youtube_view);

    FeedMetaData feedMetaData = feedMetaDatas.get(position);
    //playing Youtube Video 
   youtubePlayerControl.playVideo(playerFragment, feedMetaData.getVideoId());

    container.addView(itemView);
    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}

@Override
public Parcelable saveState() {
    return null;
}

}

1 个答案:

答案 0 :(得分:0)

您的YoutubePlayerControl课程必须延长YouTubeBaseActivity课程。否则会崩溃