gpt4 book ai didi

android - 制作全屏视频 View

转载 作者:搜寻专家 更新时间:2023-11-01 08:35:47 25 4
gpt4 key购买 nike

我正在 android 中创建我的自定义视频播放器。我需要的要求是,当设备处于纵向模式时,VideoView 的高度是屏幕的一半,当我在横向模式下旋转设备时,它应该是全屏模式。但是我无法实现这个功能

下面是我的xml代码

<RelativeLayout 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"
tools:context="dp.com.player.MainActivity">

<RelativeLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="@dimen/_200sdp"
android:id="@+id/video_frame">

<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />

<ImageView
android:id="@+id/pause"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:src="@drawable/ic_pause"
android:layout_centerInParent="true"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CC000000"
android:orientation="vertical"
android:id="@+id/layout_controller"
android:layout_alignParentBottom="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="4dip"
android:orientation="horizontal">


<TextView android:id="@+id/time_current"
android:textSize="14sp"
android:textStyle="bold"
android:paddingTop="4dip"
android:paddingLeft="4dip"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:textColor="@android:color/white"/>

<SeekBar
android:id="@+id/sick_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="32dip" />

<TextView android:id="@+id/total_duration"
android:textSize="14sp"
android:textStyle="bold"
android:paddingTop="4dip"
android:paddingRight="4dip"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dip"
android:textColor="@android:color/white"/>

<ImageView
android:id="@+id/full_screenMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_fullscreen_white_24dp"
android:paddingTop="4dip"
android:paddingRight="4dip"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingLeft="4dip"/>
</LinearLayout>



</LinearLayout>


</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/video_frame"
android:id="@+id/other_view">

</LinearLayout>
</RelativeLayout>

下面是我的 Activity 代码

public class MainActivity extends AppCompatActivity  {

private int brightnessValue;
ContentResolver contentResolver;
Window window;



VideoView videoView;
private String url = "http://aklearningsolutions.com/video/Nursery Rhymes.mp4";
SeekBar seekBar;
TextView maxTime,currentTime;
long totalDuration;
MediaPlayer mp;
Handler mHandler = new Handler();
LinearLayout mediaController;
RelativeLayout frameLayout;
ImageView pause,fullScreenImage;
boolean isFullscreen;
LinearLayout otherView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initialisedObject();
playVideo();




private void playVideo() {

Uri uri=Uri.parse(url.replace(" ","%20"));
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
updateControllerWidget(mp);
videoView.start();


}
});
}


private void initialisedObject() {
seekBar = (SeekBar)findViewById(R.id.sick_bar);
maxTime = (TextView)findViewById(R.id.total_duration);
videoView = (VideoView)findViewById(R.id.videoView);
currentTime = (TextView)findViewById(R.id.time_current);
mediaController = (LinearLayout)findViewById(R.id.layout_controller);
frameLayout = (RelativeLayout) findViewById(R.id.video_frame);
pause = (ImageView)findViewById(R.id.pause);
fullScreenImage = (ImageView)findViewById(R.id.full_screenMode);
otherView = (LinearLayout)findViewById(R.id.other_view);



}

public void fullScreenVideo(){
otherView.setVisibility(View.GONE);
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));
fullScreenImage.setImageResource(R.drawable.ic_fullscreen_exit_white_24dp);
isFullscreen = true;
hideController(isFullscreen);

}
public void exitFullscreenVideo(){
fullScreenImage.setImageResource(R.drawable.ic_fullscreen_white_24dp);
isFullscreen = false;
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT));

}



@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
exitFullscreenVideo();
}else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
fullScreenVideo();

}
}



}

最佳答案

据我了解,如果是这样的话,您希望布局在横向模式下全屏显示

在 layout-land 文件夹中为横向模式创建一个新布局

如果需要,请隐藏 ActionBar,然后在布局区域中使 VideoView match_parent 高度和宽度明智。

类似的东西

<VideoView 
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

更新:这并不完美,只是一个示例,向您展示如何保存视频位置,然后从那里继续播放

private VideoView myVideoView;
private int position = 0;

//THEN OVERRIDE THESE TWO METHODS TO SAVE THE VIDEOVIEW Position BEFORE ACTIVITY GETS RECREATED

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);

//HERE WE ARE SAVING VideoView SATATE
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);

//HERE WE GETTING THE Position BEFORE VIDEOVIEW WAS DESTROYED
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}

关于android - 制作全屏视频 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36807093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com