我正在使用youtube支持片段,因为出于某种原因加载视频,它只是无限加载。 它分为两个类,一个是片段插入一个标签式活动,另一个是视频片段(无法直接从布局中使用片段项,它将空出现并导致Struggling with Youtube Player Support Fragment)
我正在使用视频代码而不是网址,权限是正常的,互联网正在运作
有没有理由让视频永远保持加载并且不会开始播放?
public class FragmentYouTubePlayer extends YouTubePlayerSupportFragment
implements YouTubePlayer.OnInitializedListener
{
private static final int RECOVERY_DIALOG_REQUEST = 1;
public static final String ARG_KEY_VIDEO_ID="ARG_KEY_VIDEO_ID";
public static final String DEVELOPER_KEY = "a dev key in my code";
private String _videoId;
private YouTubePlayer _player;
public static FragmentYouTubePlayer newInstance(String videoId) {
FragmentYouTubePlayer playerYouTubeFrag = new FragmentYouTubePlayer();
Bundle bundle = new Bundle();
bundle.putString(ARG_KEY_VIDEO_ID, videoId);
playerYouTubeFrag.setArguments(bundle);
return playerYouTubeFrag;
}
/**
* enable the initialize of video
*/
public void initVideo()
{
initialize(DEVELOPER_KEY, this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DEVELOPER_KEY, this);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason)
{
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer (%1$s)",
errorReason.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
_player=player;
player.cueVideo(_videoId);
player.setShowFullscreenButton(false);
}
}
public YouTubePlayer.Provider getYouTubePlayerProvider() {
return this;
}
@Override
public void onDestroyView() {
if(_player!=null) {
_player.release();
}
super.onDestroyView();
}
}
从这一个中调用这个片段:
public class FragmentVideo extends Fragment
{
public static final String ARG_KEY_VIDEO_ID="ARG_KEY_VIDEO_ID";
private FragmentYouTubePlayer _youTubePlayerFragment;
private String _videoId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.l_f_video,container,false);
initData(view);
initView(view);
return view;
}
private void initData(View view) {
_videoId=((MYActivity)getActivity()).get_video();
_youTubePlayerFragment = FragmentYouTubePlayer.newInstance(_videoId);
}
private void initView(View view) {
//swap framelayout with youtube fragment
getFragmentManager().beginTransaction().
replace(R.id.l_f_st_videoFragment, _youTubePlayerFragment).commit();
_youTubePlayerFragment.initVideo();
}
}