gpt4 book ai didi

android - YouTube api : Not connected. 调用 connect() 并等待调用 onConnected()

转载 作者:可可西里 更新时间:2023-11-01 18:49:12 27 4
gpt4 key购买 nike

我在 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)

fragment 是这样开始的:

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

知道哪里出了问题吗?

最佳答案

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() {

}

}
}

关于android - YouTube api : Not connected. 调用 connect() 并等待调用 onConnected(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22296368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com