我是一个新手,所以这是我能提供的最好帮助。顺便说一句,在 Exoplayer Demo 应用程序中进行了测试,我将 exoplayer 的高度更改为 600 像素,并应用了这段代码,它完美地运行。
添加此代码以检测屏幕方向
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checking the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//First Hide other objects (listview or recyclerview), better hide them using Gone.
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) simpleExoPlayerView.getLayoutParams();
params.width=params.MATCH_PARENT;
params.height=params.MATCH_PARENT;
simpleExoPlayerView.setLayoutParams(params);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//unhide your objects here.
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) simpleExoPlayerView.getLayoutParams();
params.width=params.MATCH_PARENT;
params.height=600;
simpleExoPlayerView.setLayoutParams(params);
}
}
顺便说一句,如果您没有使用FrameLayout而是使用RelativeLayout
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) simpleExoPlayerView.getLayoutParams();
我忘记了你需要隐藏操作或标题栏,希望这段代码能够帮到你。将这些代码添加到上面的代码中,同时我认为你需要扩展你的活动到AppCompatActivity,让getSupportActionBar代码起作用。
if(getSupportActionBar()!=null) {
getSupportActionBar().hide();
}
//To show the action bar
if(getSupportActionBar()!=null) {
getSupportActionBar().show();
}
此外,这可能有助于将整个项目设置为全屏,隐藏状态栏等等,必须根据屏幕方向在onConfigurationChanged中添加。
在横向模式下
ExoPlayerActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN || View.SYSTEM_UI_FLAG_IMMERSIVE);
退出 PORTRAIT 模式下的全屏
ExoPlayerActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
我编辑了代码,添加了View.SYSTEM_UI_FLAG_IMMERSIVE以防止在用户点击视频控制按钮时状态栏显示。
android:configChanges="orientation|screenSize|keyboardHidden"
! - schneidaandroid:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
- Daiwik Dan在设置播放器时,您可以检查方向并相应地设置参数:
if (getActivity().getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
params = (LinearLayout.LayoutParams)
exoPlayerView.getLayoutParams();
params.width = params.MATCH_PARENT;
params.height = params.MATCH_PARENT;
exoPlayerView.setLayoutParams(params);
}
隐藏操作栏:
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
My solution with data binding:
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View" />
<variable
name="orientationLandscape"
type="boolean" />
<variable
name="step"
type="com.udacity.zeban.baking.data.models.Step" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/step_video"
android:layout_width="match_parent"
android:layout_height="@dimen/player_height"
app:layout_constraintBottom_toTopOf="@+id/scroll"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_height="@{orientationLandscape}" />
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/step_video">
<android.support.v7.widget.CardView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_small"
android:visibility="@{orientationLandscape ? View.GONE : View.VISIBLE}">
<TextView
android:id="@+id/step_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_small"
android:text="@{step.getDescription()}"
tools:text="@tools:sample/lorem/random" />
</android.support.v7.widget.CardView>
</ScrollView>
</android.support.constraint.ConstraintLayout>
在您的活动或片段中添加以下内容:
@BindingAdapter("layout_height")
public static void setLayoutHeight(View view, boolean orientationLandscape) {
view.getLayoutParams().height = orientationLandscape ? RelativeLayout.LayoutParams.MATCH_PARENT : 600;
}
@Override
public void onResume() {
super.onResume();
binding.setOrientationLandscape(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
}
我的项目在这里
。private void initFullscreenDialog() {
mFullScreenDialog = new Dialog(mContext, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
public void onBackPressed() {
if (mExoPlayerFullscreen) { //mExoPlayerFullscreen is a boolean to check if it is already fullscreen or not
closeFullscreenDialog();
}
super.onBackPressed();
}
};
}
mFullScreenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mExoPlayerFullscreen) {
((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
openFullscreenDialog();
} else {
((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
closeFullscreenDialog();
}
}
});
然后您可以按照以下方式打开和关闭全屏:
private void openFullscreenDialog() {
((ViewGroup) playerView.getParent()).removeView(playerView);
mFullScreenDialog.addContentView(playerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mFullScreenIcon.setImageResource(R.drawable.ic_exit_fullscreen);
mExoPlayerFullscreen = true;
mFullScreenDialog.show();
}
private void closeFullscreenDialog() {
((ViewGroup) playerView.getParent()).removeView(playerView);
((FrameLayout) findViewById(R.id.main_media_frame)).addView(playerView);
mExoPlayerFullscreen = false;
mFullScreenDialog.dismiss();
mFullScreenIcon.setImageResource(R.drawable.ic_fullscreen);
}
我在xml中使用FrameLayout作为PlayerView的父级,如下所示:
最初的回答:
<FrameLayout
android:id="@+id/main_media_frame2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/common_item_header_container"
android:background="#000000">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:focusable="true"></com.google.android.exoplayer2.ui.PlayerView>
</FrameLayout>
((FrameLayout) findViewById(R.id.main_media_frame)).addView(playerView);
是哪个 FrameLayout? - Mahmoud Mabrokpublic void onBackPressed() {
if (mExoPlayerFullscreen) { //mExoPlayerFullscreen是一个布尔值,用于检查是否已经全屏
closeFullscreenDialog();
}
super.onBackPressed();
}
- Mahmoud Mabrok使用以下代码隐藏状态栏:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
使用以下代码隐藏导航栏:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
对于有硬件按钮的屏幕,如果没有其他视图与ExoPlayer的PlayerView共享屏幕,则默认情况下它将填充整个屏幕。但是对于屏幕上有虚拟按钮的设备,它将不会填充整个屏幕。在我的情况下,通过将此行添加到PlayerView中解决了问题。
ads:resize_mode="fill"
由于我的活动布局中有AdMob广告,因此我在顶部的RelativeLayout标签中添加了这行代码。
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:resize_mode="fill"
会导致gradle构建错误。 @Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
}
}
private void hideSystemUI() {
// Enables "lean back" mode
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
// Hide the nav bar and status bar
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
欲了解更多信息,请访问Android开发者页面https://developer.android.com/training/system-ui/immersive
我认为我们在这里错过了更明显的答案。只需在 layout-land
目录中创建一个新的布局文件,以便在横向模式下应用新的布局文件。
在这个新的布局文件中,只需要将你的 ExoPlayer
的 layout_height
改为 match_parent
,这样它就会占据整个屏幕。
设置屏幕方向的最简单方法。
取一个布尔值:
boolean abc = false;
现在:
if (abc) {
list.set(position, new IconsModel(R.drawable.screenlocked, "Portrait"));
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
abc = false;
} else {
list.set(position, new IconsModel(R.drawable.screenrotate, "Landscape"));
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
abc = true;
}
我正在使用 recyclerview
,所以我使用了 list.add
。
你可以使用这行代码代替:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.yourIamge));
我在playerView中添加了:
app:resize_mode="fixed_width"
然后一切都运作良好