2016-04-23 72 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);