在viewpager中显示Youtubevideo

我想在ViewPager设置YouTubevideo。 为此,我在适配器中设置了FrameLayout ,并在其中设置了YoutubeVideoFragment,如下所示:

我的ViewPager适配器:

 import android.app.Activity; import android.app.FragmentTransaction; import android.content.Context; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; import np.com.test.R; import np.com.test.fragment.YoutubeVideoFragment; /** * Created by Vikash on 8/18/2015. */ public class TutorialVideoAdapter extends PagerAdapter { private List mList; private Activity mActivity; public TutorialVideoAdapter(Activity activity, List list) { mActivity = activity; mList = list; } @Override public int getCount() { return mList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { LayoutInflater inflater = (LayoutInflater) mActivity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.pager_video_tutorial, container, false); YoutubeVideoFragment fragment = new YoutubeVideoFragment(); Bundle bundle = new Bundle(); bundle.putString("video_key", mList.get(position)); fragment.setArguments(bundle); FragmentTransaction transaction = mActivity.getFragmentManager().beginTransaction(); transaction.add(R.id.fragment_container, fragment); transaction.commit(); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } 

在上面的适配器中, YoutubeVideoFragment用于加载Youtubevideo。

YoutubeVideoFragment:

 import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayerFragment; import np.com.test.R; import np.com.test.utils.Config; /** * Created by Vikash on 8/18/2015. */ public class YoutubeVideoFragment extends Fragment implements YouTubePlayer.OnInitializedListener { private String YOUTUBE_VIDEO_CODE; private static final int RECOVERY_DIALOG_REQUEST = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); YOUTUBE_VIDEO_CODE = getArguments().getString("video_key"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragmentYoutubeView = inflater.inflate(R.layout.fragment_youtube_video, container, false); YouTubePlayerFragment mYoutubePlayerFragment = new YouTubePlayerFragment(); mYoutubePlayerFragment.initialize(Config.GOOGLE_API_KEY, this); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_youtube_player, mYoutubePlayerFragment); fragmentTransaction.commit(); return fragmentYoutubeView; } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { if (!wasRestored) { player.cueVideo(YOUTUBE_VIDEO_CODE); } } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) { if (errorReason.isUserRecoverableError()) { errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show(); } else { String errorMessage = String.format( getString(R.string.error_player), errorReason.toString()); Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show(); } } } 

fragmenet_youtube_videopager_video_tutorial的 XML布局代码

   

我得到了什么? 仅显示第一个位置片段。 我只能在viewpager剩余页面中看到一个video是空白的。

我尝试了什么? 我试图从昨天在谷歌搜索解决方案,但无法取得成功。

我的问题:如何加载剩余的YoutubeVideoFragment

谢谢。

当传呼机包含多个YouTubePlayerFragments时,我收到了我的查看寻呼机覆盖YouTube视图的错误。

经过大量的研究,我解决了如下问题。

  • 覆盖片段的setUserVisibleHint()。

  • 释放以前的YouTubePlayer(mYoutubePlayer)。

  • 通过initialize()创建播放器的新实例。

  • 在onInitilizationSuccess中,使用youtube_video_id加载播放器。

注意:我正在使用FragmentStatePagerAdapter。

 @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (!isVisibleToUser && mYoutubePlayer != null) { mYoutubePlayer.release(); } if (isVisibleToUser && mYouTubePlayerSupportFragment != null) { mYouTubePlayerSupportFragment.initialize(API_KEY, Context); } 

受上面答案的启发,我有一个解决这个问题的工作方案。

在OnCreate()中:

 mYouTubePlayerSupportFragment = YouTubePlayerSupportFragment.newInstance(); if (getUserVisibleHint()) { Log.v (TAG, "Committing transaction, URL : " + getArguments().getString(KeyConstant.KEY_VIDEO_URL)); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.replace(R.id.fl_youtube_player, mYouTubePlayerSupportFragment).commit(); mYouTubePlayerSupportFragment.initialize(DeveloperKey.DEVELOPER_KEY, this); } 

在setUserVisibleHint()中:

 if (!isVisibleToUser && mYoutubePlayer != null) { Log.v (TAG, "Releasing youtube player, URL : " + getArguments().getString(KeyConstant.KEY_VIDEO_URL)); mYoutubePlayer.release(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.remove(mYouTubePlayerSupportFragment).commit(); } if (isVisibleToUser && mYouTubePlayerSupportFragment != null) { Log.v (TAG, "Initializing youtube player, URL : " + getArguments().getString(KeyConstant.KEY_VIDEO_URL)); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.replace(R.id.fl_youtube_player, mYouTubePlayerSupportFragment).commit(); mYouTubePlayerSupportFragment.initialize(DeveloperKey.DEVELOPER_KEY, this); } 
 public class PlayerFragment extends Fragment implements YouTubePlayer.OnInitializedListener{ YouTubePlayer player; Context context; YouTubePlayerSupportFragment youTubePlayerSupportFragment; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_layout,container,false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); youTubePlayerSupportFragment = new YouTubePlayerSupportFragment(); getChildFragmentManager().beginTransaction().replace(R.id.container,youTubePlayerSupportFragment).commit(); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (!isVisibleToUser && player != null) { player.release(); } if (isVisibleToUser && youTubePlayerSupportFragment != null) { youTubePlayerSupportFragment.initialize(YOUR_KEY, this); } } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { this.player = youTubePlayer; this.player.loadVideo("BqVoYfFrCMs"); this.player.play(); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } }