YouTubePlayerView会在屏幕方向更改时重新加载视频

时间:2013-10-17 20:37:24

标签: android android-youtube-api

我使用以下活动从我的应用中启动了YouTube视频。 启动活动后,将播放YouTube视频。但是当我改变屏幕的方向时,视频没有恢复,而是从头开始。 这有什么问题?

package com.example.blahblah;

import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

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;

public class YTPlayActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {


       static private String yt_sk;
       static private final String DEVELOPER_KEY = "my-api-key";

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.yt_player);
              Bundle extras = getIntent().getExtras();
             yt_sk = extras.getString("yt_sk");

              YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);

           youTubeView.initialize(DEVELOPER_KEY, this);

          }

         @Override

         public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {

                Toast.makeText(this, "Oh no! "+error.toString(),
Toast.LENGTH_LONG).show();

       }
       @Override

       public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {

              player.loadVideo(yt_sk);

       }



}

1 个答案:

答案 0 :(得分:29)

在AndroidManifest文件中添加了configChanges并且它有效。

例如:

 <activity android:label=Activity Name" 
  android:configChanges="keyboardHidden|orientation|screenSize"
  android:name="com.example.blahblah">