YouTube api:未连接。调用 connect() 并等待 onConnected() 被调用
Posted
技术标签:
【中文标题】YouTube api:未连接。调用 connect() 并等待 onConnected() 被调用【英文标题】:YouTube api: Not connected. Call connect() and wait for onConnected() to be called 【发布时间】:2014-04-13 07:58:05 【问题描述】:我在 android 应用中使用 YouTubeFragment,并在 Android 4+ 设备上出现以下崩溃。
java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.youtube.player.a.at.i(Unknown Source)
at com.google.android.youtube.player.a.an.k(Unknown Source)
at com.google.android.youtube.player.a.an.a(Unknown Source)
at com.google.android.youtube.player.a.ao.<init>(Unknown Source)
at com.google.android.youtube.player.a.f.a(Unknown Source)
at com.google.android.youtube.player.q.a(Unknown Source)
at com.google.android.youtube.player.a.at.g(Unknown Source)
at com.google.android.youtube.player.a.ax.a(Unknown Source)
at com.google.android.youtube.player.a.aw.a(Unknown Source)
at com.google.android.youtube.player.a.av.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5450)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
片段是这样开始的:
XYouTubeFragment yFragment = XYouTubeFragment.newInstance(ytVideoId);
activity.commitFragment(yFragment);
并定义如下:
public class XYouTubeFragment extends YouTubePlayerSupportFragment
public static XYouTubeFragment newInstance(String url)
XYouTubeFragment youTubeFrag = new XYouTubeFragment();
Bundle bundle = new Bundle();
bundle.putString("url", url);
youTubeFrag.setArguments(bundle);
youTubeFrag.init();
return youTubeFrag;
private void init()
initialize(Constants.YOUTUBE_API_KEY, new OnInitializedListener()
@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1)
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!wasRestored)
player.loadVideo(getArguments().getString("url"), 0);
);
@Override
public void onActivityCreated(Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
MainActivity activity = (MainActivity) getActivity();
activity.getSupportActionBar().hide();
activity.findViewById(R.id.alert_list).setVisibility(View.GONE);
知道可能出了什么问题吗?
【问题讨论】:
你找到这个问题的答案了吗? 同样的问题,主要发生在摩托罗拉 XT1080 和 Android 4.4.4 上。 我也面临同样的问题。你能找到这个问题的解决方案吗? 【参考方案1】:public class YoutubeManager implements YouTubePlayer.OnInitializedListener
String VideoTitle;
String VideoIdLoaded;
boolean videoTitleEnabled;
boolean isPreLoaded;
public void loadVideoWithId(String videoId)
public static final String DEFAULT_API_KEY = "Your api key";
String appKey;
String searchKey;
String videoId_local;
String userId_local;
boolean firstRun;
Activity mActivity;
YouTubePlayer mYoutubePlayer;
private YouTubeFragment youTubePlayerFragment;
private MyPlayerStateChangeListener myPlayerStateChangeListener;
private MyPlaybackEventListener myPlaybackEventListener;
public YoutubeManager(Activity activity, String key, int containerId, int windowWidth, int windowHeight)
mActivity = activity;
appKey = key;
searchKey = null;
videoId_local = null;
userId_local = null;
firstRun = true;
offsetTime = 0D;
myPlayerStateChangeListener = new MyPlayerStateChangeListener();
myPlaybackEventListener = new MyPlaybackEventListener();
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
YouTubePlayerFragment = new YouTubeFragment();
ViewGroup.MarginLayoutParams params =
new ViewGroup.MarginLayoutParams(windowWidth / 2,
windowHeight / 2);
params.setMargins(0, 0, 0, 0);
youTubePlayerFragment.setLayoutParams(params);
fragmentTransaction.add(containerId, youTubePlayerFragment);
fragmentTransaction.commit();
youTubePlayerFragment.initialize(key, this);
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored)
mYoutubePlayer = player;
mYoutubePlayer.setPlayerStateChangeListener(myPlayerStateChangeListener);
mYoutubePlayer.setPlaybackEventListener(myPlaybackEventListener);
if (!wasRestored)
mYoutubePlayer.cueVideo("nCgQDjiotG0");
else
//TEST of video playback
mYoutubePlayer.cueVideo("nCgQDjiotG0");
play(null);
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason)
if (errorReason.isUserRecoverableError())
//errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
else
//String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
//Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
public void loadVideoWithId(String videoToLoad)
videoId_local = videoToLoad;
mYoutubePlayer.cueVideo(videoId_local);
//Users
public String getLocalUserID()
return videoId_local;
;
public void addUser(String userId)
;
public void removeUser(String userId)
public void removeAllUsers()
//External Playback
public void play(String userId)
mYoutubePlayer.play();
public void pause(String userId)
mYoutubePlayer.pause();
public void stop(String userId)
public void close(String userId)
private final class MyPlayerStateChangeListener implements PlayerStateChangeListener
@Override
public void onAdStarted()
Log.INFO("YOUTUBE", "onAdStarted()");
@Override
public void onError(
com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0)
Log.INFO("YOUTUBE", "onError(): " + arg0.toString());
@Override
public void onLoaded(String arg0)
Log.INFO("YOUTUBE", "onLoaded(): " + arg0);
@Override
public void onLoading()
Log.INFO("YOUTUBE", "onLoading()");
@Override
public void onVideoEnded()
Log.INFO("YOUTUBE", "onVideoEnded()");
@Override
public void onVideoStarted()
Log.INFO("YOUTUBE", "onVideoStarted()");
private final class MyPlaybackEventListener implements PlaybackEventListener
@Override
public void onBuffering(boolean arg0)
Log.INFO("YOUTUBE", "onBuffering(): " + String.valueOf(arg0));
@Override
public void onPaused()
@Override
public void onPlaying()
@Override
public void onSeekTo(int arg0)
@Override
public void onStopped()
【讨论】:
请解释(简而言之)你在那里做了什么,否则人们不会觉得它有多大用处。谢谢! 请解释你在这个解决方案中做了什么来克服这个问题。你的回答没有用。 如果你只需要 youtube 片段然后使用这个 sn-p-youTubePlayerFragment.setLayoutParams(params); 以上就是整个类fragmentTransaction.add(containerId, youTubePlayerFragment); fragmentTransaction.commit(); youTubePlayerFragment.initialize(key, this);以上是关于YouTube api:未连接。调用 connect() 并等待 onConnected() 被调用的主要内容,如果未能解决你的问题,请参考以下文章