2016-04-23 121 views
2

我在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(); 

     } 
    } 



} 
+0

use setRetainInstance(true);在onCreate它会保持你的状态时,方向变化 –

回答

0

你提到的android:layout_height = “@扪/ _200sdp” 如此明显它并没有全屏显示

创建layout-land文件夹并将下面的代码放在相同的文件名和相同的编号下

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <VideoView android:id="@+id/video" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     /> 
</RelativeLayout> 
+0

,但我们需要在纵向模式下修复videoView 200的高度,为了达到这个要求我修复了这个布局的高度200 –

1

你应该在新的布局layout-land directory再创建一个布局,景观视野
enter image description here

然后(景观设计)

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"// change height to match_parent 
    android:orientation="vertical" > 

    <VideoView android:id="@+id/video" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     /> 
</RelativeLayout> 

如果你不想让你的布局重新创建时旋转landcapse,你应该:
+通过onConfigurationChanged方法检测取向变化
+检查方向是纵向状态>更改RelativeLayout = 200高度编程
+定向景观 - >变化RelativeLayout = MATCH_PARENT

+0

但我们需要将videoView 200的高度固定在肖像模式,为了达到这个要求,我修正了这个布局200的高度。如果我们使用match_parent,它在纵向模式下显示全屏VideoView –

+0

我创建了layout-land,但它不工作。为了工作,我们从清单文件中移除了android:configChanges =“orientation | keyboardHidden | screenSize | layoutDirection”,结果是当我们旋转屏幕视频时将从开始播放。 –

+0

@ shaileshojha请看我的更新答案。希望它可以给你一个想法来解决你的问题 –

2

据我了解你想要的布局是在横向模式全屏幕,如果是这样

创建布局,土地文件夹

横向模式的新布局隐藏ActionBar如果需要,然后进行VideoView match_parent高度和宽度布局土地明智的。

类似的东西

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

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

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); 
} 
+0

我创建了layout-land,但它不起作用。为了工作,我们从清单文件中移除了android:configChanges =“orientation | keyboardHidden | screenSize | layoutDirection”,结果是当我们旋转屏幕视频时将从开始播放。 –

+0

实际情况是当你旋转屏幕时活动被重新创建,所以你需要重写'onSaveInstanceState'来保存视频位置,找到了我? – Max

+0

你可以解释更多的细节我们如何使用onSaveInstanceState来保存视频位置 –

0

可能becozz你已经修复了你的相对布局高度

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

使它变成match_parent和videoview wrap_content

,并在活动

   // full screen 
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
         WindowManager.LayoutParams.FLAG_FULLSCREEN); 

添加此标志,如果你想隐藏SystemUI底部导航栏,然后用这个。,

    // Set the IMMERSIVE flag. 
      // Set the content to appear under the system bars so that the content 
      // doesn't resize when the system bars hide and show. 
      int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
        | View.SYSTEM_UI_FLAG_FULLSCREEN; 
      getWindow().getDecorView().setSystemUiVisibility(uiOptions);