Android Youtube Player Api - ERROR_CONNECTING_TO_S

2020-02-14 20:47发布

问题:

I'm trying to use the new api youtube. I followed the examples provided, but I get the error: ERROR_CONNECTING_TO_SERVICE

The "API key" that I'm using is correct (since it works correctly in the google demos).

Imports:

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

Classe definition:

public class Track extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

     private YouTubePlayerView ytpv;
     private YouTubePlayer ytp;

     public void onCreate(Bundle savedInstanceState) {
          ...

          ytpv = (YouTubePlayerView) findViewById(R.id.youtube_view);
          ytpv.initialize("my_api_key_correctly_created", this); 
     }

     public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
          Toast.makeText(this, "Initialization Fail" + arg1.toString(), Toast.LENGTH_LONG).show();
     }


     public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1, boolean arg2) {
          ytp = arg1;
          Toast.makeText(this, "Initialization  Success", Toast.LENGTH_LONG).show();

          if(ytp !=null)
          {
               ytp.loadVideo("ZvvgDC_MNqI");
          }
     }
}

What am I doing wrong? Thank you, and sorry my english:)

回答1:

Are you trying to start the 'Track' Activity as part of an ActivityGroup/TabActivity inside a TabHost? If so, unfortunately the YouTubePlayerView or YouTubePlayerFragment can't be initialized inside an ActivityGroup. Since the ActivityGroup is now deprecated, the only suggestion I can give is to use Fragments instead.