0
我在Viewpager中加載Youtube視頻,但如果視圖中有兩個視頻傳呼機只有第二個視頻能夠播放。請幫助 如果只有最後一個視頻可以播放,那麼我想加載視圖分頁器中的所有視頻。YoutubePlayer與ViewPager在PagerAdapter
YoutubeVideo裝載器類: -
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;
}
}
MainActivity擴展YouTubeBaseActivity,YoutubePlayerControl用於控制播放選項Only。 – KnowAmit