title: Android YouTube API “An error occurred while initializing YouTube player”
body: <p>I have a Fragment containing a <a href="https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerSupportFragment" rel="nofollow">YouTubePlayerSupportFragment</a>. This fragment is inside a ViewPager. </p>
  
  <p>The player works fine on my Galaxy S3 Android phone (4.3 / API 18) and on my VM (4.3 / API 18).
  But on every other phones i've tried, the player shows a <code>An error occurred while initializing YouTube player</code> message and throw a <a href="https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer.OnInitializedListener#onInitializationFailure(com.google.android.youtube.player.YouTubePlayer.Provider,%20com.google.android.youtube.player.YouTubeInitializationResult)" rel="nofollow">onInitializationFailure</a> with an <code>UNKNOWN_ERROR</code> reason.</p>
  
  <p>I've checked the LogCat and there is absolutly no other error/warning, from my app or from youtube.</p>
  
  <p>It didnt work on the following devices:</p>
  
  <ul>
  <li>Android 4.2.1 - Youtube 10.18.55 </li>
  <li>Android 4.4.2 - Youtube 10.18.55</li>
  <li>Android 5.1 - Youtube above requirement</li>
  </ul>
  
  <p>I have absolutely no idea why it worked on some and why it didn't on others.</p>
  
  <p>Here is the code of the Fragment:</p>
  
  <pre><code>    public class RoomFragmentVideo extends PlugFragment implements
      YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener {
              private static String CLASS_DBG_NAME = "RoomFragmentVideo";
  
              private static final int RECOVERY_DIALOG_REQUEST = 1;
  
              private YouTubePlayerSupportFragment youTubePlayer ;
              private int playerInitialized;
              private YouTubePlayer player;
  
              static PlugFragment init(Bundle args) {
                       RoomFragmentVideo roomFrag = new RoomFragmentVideo();
                       // Supply val input as an argument.
                       roomFrag.setArguments(args);
                       Log.d(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - ","Created with args Room: "  + ((String)args.get(RoomFragmentChat.ARG_ROOM)));
                       return roomFrag;
              }
  
              @Override
              public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               thisActivity = (AppCompatActivity) this.getActivity();
              }
  
              @Override
              public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
                       View rootview = inflater.inflate(R.layout.fragment_roomvideo, container,false);
                       youTubePlayer = YouTubePlayerSupportFragment.newInstance();
                       FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                       transaction.add(R.id.youtube_fragment, youTubePlayer).commit();
                       youTubePlayer.initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
                       return rootview;
  
              }
  
              protected YouTubePlayer.Provider getYouTubePlayerProvider() {
                  return youTubePlayer;
              }
  
              @Override
              public void onActivityResult(int requestCode, int resultCode, Intent data) {
                  if (requestCode == RECOVERY_DIALOG_REQUEST) {
                    getYouTubePlayerProvider().initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
                  }
              }         
  
              @Override
              public void onInitializationFailure(YouTubePlayer.Provider provider,
                            YouTubeInitializationResult errorReason) {
                      if (errorReason.isUserRecoverableError()) {
                            errorReason.getErrorDialog(this.getActivity(), 1).show();
                            Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Recoverable] Reason : " + errorReason.toString());
                      }
                      else {
                              //String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
                              String errorMessage = "Video couldnt load";
                              Toast.makeText(this.getActivity(), errorMessage, Toast.LENGTH_LONG).show();
                              Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Unrecoverable] Reason : " + errorReason.toString() + " Message: " + errorMessage);
                              playerInitialized = -1; 
                      }
  
              }
  
              @Override
              public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                            boolean wasRestored) {
                              this.player = player;
                              player.setPlayerStyle(PlayerStyle.CHROMELESS);
                              player.loadVideo(this.videoID);
                              playerInitialized = 1;
              }
  
              @Override
              public void onPause(){
                      super.onPause();
                      Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onPause");
              }
  
              @Override
              public void onResume(){
                      super.onResume();
                      Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onResume");
              }
  
              @Override
              public void onStop(){
                      super.onStop();
                      player = null;
                      playerInitialized = -2;
                      Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onStop");
              }
  
              @Override
              public void setUserVisibleHint (boolean isVisibleToUser){
                      super.setUserVisibleHint(isVisibleToUser);
                      if(isVisibleToUser){
                              this.resume();
                      }
                      Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "setUserVisibleHint, status: " + isVisibleToUser);
              }
  
              @Override
              public void onAdStarted() {
                      return;                
              }
  
              @Override
              public void onError(ErrorReason arg0) {
                      Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " +"YoutubePlayer", arg0.name());
                      Toast.makeText(thisActivity, "YoutubePlayer error: " + arg0.name(), Toast.LENGTH_SHORT).show();
              }
  
              @Override
              public void onLoaded(String arg0) {
                      return;
              }
  
              @Override
              public void onLoading() {
                      return;
              }
  
              @Override
              public void onVideoEnded() {
                      return;
              }
  
              @Override
              public void onVideoStarted() {
                      return;
  
              }
      }
  </code></pre>
  
  <p>And here is the XML:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" &gt;
  
         &lt;FrameLayout
              android:id="@+id/youtube_fragment"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom"
              android:visibility="visible" /&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>What am I doing wrong ?</p>
  
  <p>Thanks a lot !</p>