无法播放不同活动的Youtube视频

时间:2013-12-05 10:51:12

标签: android youtube android-youtube-api

我正在尝试使用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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您创建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"/>