畫像中的 YoutubePlayerFragment Activty
以下程式碼實現了一個簡單的 YoutubePlayerFragment。活動的佈局在縱向模式下被鎖定,當方向改變或使用者在 YoutubePlayer 上點選全屏時,它會轉到 lansscape,YoutubePlayer 會填滿螢幕。YoutubePlayerFragment 不需要擴充套件 Youtube 庫提供的活動。它需要實現 YouTubePlayer.OnInitializedListener 才能初始化 YoutubePlayer。所以我們的 Activity 類如下
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;
public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY ;
public static final String VIDEO_ID = "B08iLAtS3AQ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
.findFragmentById(R.id.youtubeplayerfragment);
youTubePlayerFragment.initialize(API_KEY, this);
}
/**
*
* @param provider The provider which was used to initialize the YouTubePlayer
* @param youTubePlayer A YouTubePlayer which can be used to control video playback in the provider.
* @param wasRestored Whether the player was restored from a previously saved state, as part of the YouTubePlayerView
* or YouTubePlayerFragment restoring its state. true usually means playback is resuming from where
* the user expects it would, and that a new video should not be loaded
*/
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION |
YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
if(!wasRestored) {
youTubePlayer.cueVideo(VIDEO_ID);
}
}
/**
*
* @param provider The provider which failed to initialize a YouTubePlayer.
* @param error The reason for this failure, along with potential resolutions to this failure.
*/
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
final int REQUEST_CODE = 1;
if(error.isUserRecoverableError()) {
error.getErrorDialog(this,REQUEST_CODE).show();
} else {
String errorMessage = String.format("There was an error initializing the YoutubePlayer (%1$s)", error.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
}
可以將 YoutubePlayerFragment 新增到活動的佈局 xaml 中,如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<fragment
android:id="@+id/youtubeplayerfragment"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="This is a YoutubePlayerFragment example"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="This is a YoutubePlayerFragment example"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="This is a YoutubePlayerFragment example"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="This is a YoutubePlayerFragment example"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="This is a YoutubePlayerFragment example"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="This is a YoutubePlayerFragment example"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="This is a YoutubePlayerFragment example"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
最後,你需要在活動標記內的 Manifest 檔案中新增以下屬性
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"