2015-04-19 203 views
4

我正在使用視頻視圖進行直播,我希望使此視頻切換成全屏模式,然後返回到MXPlayer或YouTube播放器等小屏幕,而不會停止視頻流。如何使用VideoView Android切換FullScreen

<ScrollView 
    android:id="@+id/scrollview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/ad_container" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="@android:color/transparent" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" > 

     <TextView 
      android:id="@+id/scroll_annouc" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:background="@color/red" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:lines="1" 
      android:marqueeRepeatLimit="marquee_forever" 
      android:padding="@dimen/ten_dp" 
      android:scrollHorizontally="true" 
      android:singleLine="true" 
      android:text="@string/app_name" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="@android:color/white" 
      android:textStyle="bold" /> 

     <LinearLayout 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/scroll_annouc" 
      android:background="@android:color/transparent" 
      android:orientation="vertical" > 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="270dp" 
       android:background="@android:color/black" > 

       <TextView 
        android:id="@+id/error_overlay" 
        android:layout_width="match_parent" 
        android:layout_height="270dp" 
        android:layout_alignParentTop="true" 
        android:background="@color/red_trans" 
        android:gravity="center" 
        android:text="@string/error_text" 
        android:textColor="@android:color/white" 
        android:textStyle="bold" 
        android:visibility="invisible" /> 

       <VideoView 
        android:id="@+id/player" 
        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" /> 

       <RelativeLayout 
        android:id="@+id/media_controller" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@color/black_trans" > 

        <ImageView 
         android:id="@+id/btn_playpause" 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:layout_alignParentLeft="true" 
         android:layout_centerVertical="true" 
         android:background="@drawable/btn_pause" 
         android:contentDescription="@string/app_name" 
         android:padding="@dimen/five_dp" /> 

        <Button 
         android:id="@+id/external_player" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:layout_centerVertical="true" 
         android:background="@null" 
         android:text="Use External Player" 
         android:textColor="@android:color/white" 
         android:textSize="@dimen/content_size" 
         android:textStyle="bold" /> 

        <ImageView 
         android:id="@+id/btn_fullscreen" 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:layout_alignParentRight="true" 
         android:layout_centerVertical="true" 
         android:contentDescription="@string/app_name" 
         android:padding="@dimen/five_dp" 
         android:src="@drawable/enter_fullscreen" /> 
       </RelativeLayout> 
      </RelativeLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/two_dp" 
       android:background="@color/app_blue" /> 

      <TextView 
       android:id="@+id/loading_txt" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:paddingBottom="@dimen/two_dp" 
       android:paddingLeft="@dimen/ten_dp" 
       android:paddingRight="@dimen/ten_dp" 
       android:paddingTop="@dimen/two_dp" 
       android:text="@string/app_name" 
       android:textColor="@android:color/white" 
       android:textSize="@dimen/content_size" 
       android:visibility="invisible" /> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/one_dp" 
       android:background="@color/light_grey" /> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/one_dp" 
       android:background="@color/light_grey" /> 

      <LinearLayout 
       android:id="@+id/channel_links_container" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:orientation="horizontal" 
       android:padding="@dimen/ten_dp" > 
      </LinearLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/one_dp" 
       android:background="@color/light_grey" /> 

      <HorizontalScrollView 
       android:id="@+id/horizontal_view" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:fillViewport="true" 
       android:scrollbars="horizontal" > 

       <LinearLayout 
        android:id="@+id/viewsContainer" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" > 
       </LinearLayout> 
      </HorizontalScrollView> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/one_dp" 
       android:background="@color/light_grey" /> 
     </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 

一切正常像流媒體視頻和所有,但我無法使這個視頻視圖去景觀和顯示全屏視頻。我曾嘗試使用Google搜索,但我發現的所有示例目前都不適合我。所以請告訴我如何切換全屏。 謝謝。

+0

檢查這個答案http://stackoverflow.com/questions/5640025/videoview-fullscreen-on-雙擊 –

+0

這是我的例子:https://github.com/vuhung3990/sampleVideoFullscreen,它將全屏當風景 – vuhung3990

回答

3

我所做的是我創建的延伸VideoView這樣的CustomVideoView:

public class CustomVideoView extends VideoView { 

private int measuredWidth = 0; 
private int measuredHeight = 0; 

public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    // TODO Auto-generated constructor stub 
} 

public CustomVideoView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
} 

public CustomVideoView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

public void setNewDimension(int width, int height) { 
    this.measuredHeight = height; 
    this.measuredWidth = width; 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // TODO Auto-generated method stub 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    setMeasuredDimension(measuredWidth, measuredHeight); 
} 
}//end class 

然後在我的玩家活動,我實現了這個上全屏按鈕的onclick :

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
      android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, 
      android.widget.RelativeLayout.LayoutParams.MATCH_PARENT); 
    player.setNewDimension(metrics.widthPixels, metrics.heightPixels); 
    player.getHolder().setFixedSize(metrics.heightPixels, 
      metrics.widthPixels); 
    player.setLayoutParams(params); 

所以這個問題解決了。希望它能解決別人的問題。

注意:我很抱歉沒有給予任何人信用,因爲我忘記了我在谷歌上找到的鏈接,這導致我爲自己做出了合適的解決方案。

+0

即時做同樣的事情,但問題是我有我的videoview頂部另一個linearlayout和佈局是對齊父母底部-----> true,但一旦我的orienatation是風景....那麼視頻應採取匹配的父母的高度和寬度和佈局應該在視圖頂部應該調整底部,但佈局並不完美如何解決這個問題? – Erum

+0

@Erum很好,你可以爲videoview和你的linearlayout創建一個包裝佈局,然後在方向改變時改變這個包裝佈局的參數 – awaistoor

+0

我已經做了同樣的事情......但是視頻視圖沒有采用全屏高度這就是爲什麼出現問題@awaistoor – Erum

2

要全屏播放視頻,您必須創建如下所示的單獨活動。 的半屏活動 -

public class HalfScreen extends Activity { 
     Button fullbtn; 
     VideoView videoView = null; 
     final int REQUEST_CODE = 5000; 
     // "https://www.youtube.com/embed/olsO7UJemhE"; 
     final String videoToPlay = "http://bffmedia.com/bigbunny.mp4"; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.half); 
      videoView = (VideoView) findViewById(R.id.VideoViewhalf); 
      final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); 
      fullbtn = (Button) findViewById(R.id.btnfullScreen); 

      Uri video = Uri.parse(videoToPlay); 
      videoView.setVideoURI(video); 
      videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
       public void onPrepared(MediaPlayer mp) { 
        progressBar.setVisibility(View.GONE); 
        videoView.requestFocus(); 
        videoView.start(); 
       } 
      }); 

      fullbtn.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Intent videointent = new Intent(HalfScreen.this, 
          FullScreen.class); 
        videointent.putExtra("currenttime", 
          videoView.getCurrentPosition()); 
        videointent.putExtra("Url", videoToPlay); 
        startActivityForResult(videointent, REQUEST_CODE); 

       } 
      }); 
     } 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) { 
       if (data.hasExtra("currenttime")) { 
        int result = data.getExtras().getInt("currenttime", 0); 
        if (result > 0) { 
         if (null != videoView) { 
          videoView.start(); 
          videoView.seekTo(result); 
          ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); 
          progressBar.setVisibility(View.VISIBLE); 

         } 
        } 
       } 
      } 
     } 
    } 


//Now the full screen activity 

    public class FullScreen extends Activity { 
      Button btn; 
      VideoView videoView = null; 
      int currenttime = 0; 
      String Url=""; 
      private static ProgressDialog progressDialog; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       // TODO Auto-generated method stub 
       super.onCreate(savedInstanceState); 

       Bundle extras = getIntent().getExtras(); 
       if (null != extras) { 
        currenttime = extras.getInt("currenttime", 0); 
        Url=extras.getString("Url"); 
       } 
       setContentView(R.layout.full); 
       progressDialog = ProgressDialog.show(this, "", "Loading...", true); 
       videoView = (VideoView) findViewById(R.id.VideoViewfull); 
       MediaController mediaController = new MediaController(this); 
       mediaController.setAnchorView(videoView); 

       Uri video = Uri.parse(Url); 
       videoView.setMediaController(mediaController); 
       videoView.setVideoURI(video); 
       videoView.setOnPreparedListener(new OnPreparedListener() { 
        public void onPrepared(MediaPlayer arg0) { 
         progressDialog.dismiss(); 
         videoView.start(); 
         videoView.seekTo(currenttime); 
        } 
       }); 
      } 

      @Override 
      public void finish() { 
       Intent data = new Intent(); 
       data.putExtra("currenttime", videoView.getCurrentPosition()); 
       setResult(RESULT_OK, data); 
       super.finish(); 
      } 
     } 

//To make the video full screen use below layout 
full.xml 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ff99cc" 
    android:orientation="vertical" > 

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

</RelativeLayout> 
+1

基本上即時通訊流視頻視圖中的直播頻道,我試過這種解決方案,但是當我從小屏幕到完整,流開始重新加載,我不想 – awaistoor

+0

是@awaistoor這是在不同佈局的情況下的主要問題 – Erum

0

我知道這太晚了,但我只是寫這個給其他隊友。

只是這行添加到您的清單活動,以避免重新加載視頻

android:configChanges="orientation|screenSize|keyboardHidden" 

希望它有助於

+0

嘗試使視圖在全屏中顯示以使佈局全屏 –