gpt4 book ai didi

android - 无法在其他 Activity 上播放Youtube视频

转载 作者:行者123 更新时间:2023-12-03 06:31:13 27 4
gpt4 key购买 nike

我正在尝试使用Youtube API PlayMedia extends YouTubeFailureRecoveryActivity播放youtube视频。
应用程序的导航
简介>>预览>> PlayMedia

我希望即使在预览应用程序无法播放时也可以播放视频。再次当我去PlayMedia我得到

12-05 14:47:41.879: E/AndroidRuntime(24195): FATAL EXCEPTION: main
12-05 14:47:41.879: E/AndroidRuntime(24195): java.lang.IllegalStateException: This YouTubePlayer has been released
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.apps.youtube.api.jar.a.a.aa(SourceFile:138)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.apps.youtube.api.jar.a.a.l(SourceFile:364)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.internal.h.onTransact(SourceFile:150)
12-05 14:47:41.879: E/AndroidRuntime(24195): at android.os.Binder.transact(Binder.java:279)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.internal.d$a$a.c(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.internal.s.isPlaying(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.myproject.android.PlayMedia.onInitializationSuccess(PlayMedia.java:315)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.YouTubePlayerView$1.a(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.internal.r.g(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.internal.r$c.a(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.internal.r$b.a(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.google.android.youtube.player.internal.r$a.handleMessage(Unknown Source)
12-05 14:47:41.879: E/AndroidRuntime(24195): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 14:47:41.879: E/AndroidRuntime(24195): at android.os.Looper.loop(Looper.java:130)
12-05 14:47:41.879: E/AndroidRuntime(24195): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-05 14:47:41.879: E/AndroidRuntime(24195): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 14:47:41.879: E/AndroidRuntime(24195): at java.lang.reflect.Method.invoke(Method.java:507)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-05 14:47:41.879: E/AndroidRuntime(24195): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-05 14:47:41.879: E/AndroidRuntime(24195): at dalvik.system.NativeStart.main(Native Method)

代码
private static YouTubePlayer video_player = new YouTubePlayer() {

@Override
public void setShowFullscreenButton(boolean arg0) {
}

@Override
public void setPlaylistEventListener(PlaylistEventListener arg0) {
}

@Override
public void setPlayerStyle(PlayerStyle arg0) {
}

@Override
public void setPlayerStateChangeListener(PlayerStateChangeListener arg0) {
}

@Override
public void setPlaybackEventListener(PlaybackEventListener arg0) {
}

@Override
public void setOnFullscreenListener(OnFullscreenListener arg0) {
}

@Override
public void setManageAudioFocus(boolean arg0) {
}

@Override
public void setFullscreenControlFlags(int arg0) {
}

@Override
public void setFullscreen(boolean arg0) {
}

@Override
public void seekToMillis(int arg0) {
}

@Override
public void seekRelativeMillis(int arg0) {
}

@Override
public void release() {
}

@Override
public void previous() {
}

@Override
public void play() {
}

@Override
public void pause() {
}

@Override
public void next() {
}

@Override
public void loadVideos(List<String> arg0, int arg1, int arg2) {
}

@Override
public void loadVideos(List<String> arg0) {
}

@Override
public void loadVideo(String arg0, int arg1) {
}

@Override
public void loadVideo(String arg0) {
}

@Override
public void loadPlaylist(String arg0, int arg1, int arg2) {
}

@Override
public void loadPlaylist(String arg0) {
}

@Override
public boolean isPlaying() {
return false;
}

@Override
public boolean hasPrevious() {
return false;
}

@Override
public boolean hasNext() {
return false;
}

@Override
public int getFullscreenControlFlags() {
return 0;
}

@Override
public int getDurationMillis() {
return 0;
}

@Override
public int getCurrentTimeMillis() {
return 0;
}

@Override
public void cueVideos(List<String> arg0, int arg1, int arg2) {
}

@Override
public void cueVideos(List<String> arg0) {
}

@Override
public void cueVideo(String arg0, int arg1) {
}

@Override
public void cueVideo(String arg0) {
}

@Override
public void cuePlaylist(String arg0, int arg1, int arg2) {
}

@Override
public void cuePlaylist(String arg0) {
}

@Override
public void addFullscreenControlFlag(int arg0) {
}
};

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,boolean wasRestored) {
if (!wasRestored) {
if(!video_player.isPlaying()){
player.cueVideo(video_URL);
video_player=player;
}
}
}

最佳答案

您创建YouTubePlayer对象而不是创建对象的方式
尝试以下代码:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center_horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello YouTube API"
android:layout_margin="12dip"/>

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dip"/>

</LinearLayout>

MainActivity.java
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {

static private final String DEVELOPER_KEY = "add your own key here!";
static private final String VIDEO = "4SK0cUNMnMM";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
}

@Override

public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
Toast.makeText(this, "Error : " + error.toString(), Toast.LENGTH_LONG).show();
}

@Override

public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
player.loadVideo(VIDEO);
}

}

AndroidManifest.xml中的 权限
<uses-permission android:name="android.permission.INTERNET"/>

关于android - 无法在其他 Activity 上播放Youtube视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20397988/

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