我正在尝试使用YouTube
直接在我的应用中播放API
视频。我能够成功播放视频,但是,我需要全屏播放视频,目前,我可以点击一个按钮全屏,当我尝试自动执行时,我收到错误。以下是活动的代码:
public class YouPlayer extends YouTubeFailureRecoveryActivity implements
View.OnClickListener,
CompoundButton.OnCheckedChangeListener,
YouTubePlayer.OnFullscreenListener {
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
private LinearLayout baseLayout;
private YouTubePlayerView playerView;
private YouTubePlayer player;
private Button fullscreenButton;
private CompoundButton checkbox;
private View otherViews;
private boolean fullscreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_player);
baseLayout = (LinearLayout) findViewById(R.id.layout);
playerView = (YouTubePlayerView) findViewById(R.id.player);
fullscreenButton = (Button) findViewById(R.id.fullscreen_button);
checkbox = (CompoundButton) findViewById(R.id.landscape_fullscreen_checkbox);
otherViews = findViewById(R.id.other_views);
checkbox.setOnCheckedChangeListener(this);
// You can use your own button to switch to fullscreen too
fullscreenButton.setOnClickListener(this);
playerView.initialize(DeveloperKey.DEVELOPER_KEY, this);
doLayout();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
this.player = player;
setControlsEnabled();
// Specify that we want to handle fullscreen behavior ourselves.
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
player.setOnFullscreenListener(this);
if (!wasRestored) {
player.cueVideo("9l7wiqEGrfc");
}
}
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return playerView;
}
@Override
public void onClick(View v) {
player.setFullscreen(!fullscreen);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int controlFlags = player.getFullscreenControlFlags();
if (isChecked) {
setRequestedOrientation(PORTRAIT_ORIENTATION);
controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
}
player.setFullscreenControlFlags(controlFlags);
}
private void doLayout() {
LinearLayout.LayoutParams playerParams =
(LinearLayout.LayoutParams) playerView.getLayoutParams();
if (fullscreen) {
// When in fullscreen, the visibility of all other views than the player should be set to
// GONE and the player should be laid out across the whole screen.
playerParams.width = LayoutParams.MATCH_PARENT;
playerParams.height = LayoutParams.MATCH_PARENT;
otherViews.setVisibility(View.GONE);
} else {
otherViews.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams otherViewsParams = otherViews.getLayoutParams();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
playerParams.width = otherViewsParams.width = 0;
playerParams.height = WRAP_CONTENT;
otherViewsParams.height = MATCH_PARENT;
playerParams.weight = 1;
baseLayout.setOrientation(LinearLayout.HORIZONTAL);
} else {
playerParams.width = otherViewsParams.width = MATCH_PARENT;
playerParams.height = WRAP_CONTENT;
playerParams.weight = 0;
otherViewsParams.height = 0;
baseLayout.setOrientation(LinearLayout.VERTICAL);
}
setControlsEnabled();
}
}
private void setControlsEnabled() {
checkbox.setEnabled(player != null
&& getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
fullscreenButton.setEnabled(player != null);
}
@Override
public void onFullscreen(boolean isFullscreen) {
fullscreen = isFullscreen;
doLayout();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
doLayout();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_you_player, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/other_views"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small"
android:text="@string/fullscreen_tutorial"/>
<CheckBox
android:id="@+id/landscape_fullscreen_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/landscape_fullscreen"/>
<Button
android:id="@+id/fullscreen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/enter_fullscreen"/>
</LinearLayout>
</LinearLayout>
所以我的问题是如何在活动启动后立即全屏播放视频?谢谢,