1

I'm using youtube support fragment in order to load a video for some reason it just loads without end. its sepetated to two classes one is a fragment insede a tabbed activity and the other is the video fragment (couldnt use the fragment item directlly from the layout it casued null exeptions and led to Struggling with Youtube Player Support Fragment)

im using the video code and not the url as supposed to, the permissions are ok and internet is working

is there a reason the video just keeps loading forever and wont start playing?

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();
     }
}

this fragment is called from this one:

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();
    }


}
Community
  • 1
  • 1
Gabriel H
  • 1,558
  • 2
  • 14
  • 35

0 Answers0