我在android中創建自定義視頻播放器。我需要當設備處於肖像模式時,VideoView
的高度是屏幕的一半,當我在橫向模式下旋轉設備時,它應該是全屏模式。但我不能沒有實現這一功能make fullScreenVideoView
是我的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>
,下面將我的活動代碼
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();
}
}
}
use setRetainInstance(true);在onCreate它會保持你的狀態時,方向變化 –