一個Activity主要用來作播放視頻使用,而且是全屏播放的話,主要採用橫屏(Landscape orientation 顯示寬度大於高度)顯示視頻,那麼能夠指定Activity的屬性android:screenOrientation="landscape"讓Activity在設備上以橫屏顯示。java
本文使用VideoView來顯示視屏,Potrait(豎屏)時佈局樣式中寬匹配父佈局,高匹配內容;Landscape(橫屏)時佈局樣式中寬匹配內容,高匹配佈局。視屏播放中,用戶調正設備方向時,致使屏幕方向發生變化,視屏可以適應佈局樣式顯示視頻,並正常繼續播放。android
示例圖:app
1. 構建佈局ide
Potrait: res/layout/activity_videoview.xml佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="5dp"/> </LinearLayout>
Landscape: res/layout-land/activity_videoview.xmlthis
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/videoView" android:layout_gravity="center_horizontal" android:layout_margin="5dp"/> </LinearLayout>
2.建立Activityspa
package secondriver.sdk.activity; import android.app.Activity; import android.app.ProgressDialog; import android.content.res.Configuration; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; import secondriver.sdk.R; /** * Author : secondriver * Created : 2015/11/30 */ public class VideoViewActivity extends Activity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener { private final String TAG = VideoViewActivity.class.getName(); public VideoView videoView; public MediaController mediaController; public int videoPosition = 0; public ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_videoview); dialog = new ProgressDialog(this); dialog.setTitle("視屏播放器"); dialog.setMessage("正在加載..."); dialog.setCancelable(false); mediaController = new MediaController(this); videoView = (VideoView) findViewById(R.id.videoView); videoView.setMediaController(mediaController); videoView.setOnCompletionListener(this); videoView.setOnPreparedListener(this); videoView.setOnErrorListener(this); } private void loadVideo() { Log.d(TAG, "load video"); dialog.show(); try { videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bsg)); } catch (Exception e) { Log.e(TAG, e.getMessage()); } } @Override protected void onStart() { Log.d(TAG, "onStart"); super.onStart(); loadVideo(); } @Override public void onConfigurationChanged(Configuration newConfig) { Log.d(TAG, "onConfigurationChanged"); super.onConfigurationChanged(newConfig); } @Override public void onCompletion(MediaPlayer mp) { Log.d(TAG, "Media onCompletion"); Toast.makeText(VideoViewActivity.this, "播放完成", Toast.LENGTH_LONG).show(); mp.release(); } @Override public void onPrepared(MediaPlayer mp) { Log.d(TAG, "Media onPrepared"); if (dialog.isShowing()) { dialog.dismiss(); } mp.seekTo(videoPosition); if (videoPosition == 0) { mp.start(); } else { mp.pause(); } } @Override public boolean onError(MediaPlayer mp, int what, int extra) { Log.d(TAG, "Media onError"); String err = "未知錯誤"; switch (what) { case MediaPlayer.MEDIA_ERROR_UNKNOWN: break; case MediaPlayer.MEDIA_ERROR_SERVER_DIED: err = "媒體服務終止"; break; default: break; } Toast.makeText(VideoViewActivity.this, err, Toast.LENGTH_LONG).show(); return true; } }
3. 設置Activity屬性.net
<activity android:name=".activity.VideoViewActivity" android:configChanges="orientation|screenSize|keyboardHidden" />
代碼中重寫了onConfigurationChanged,能夠在此處作配置發生變化的處理。視頻
在運行時發生配置更改,Activity被關閉,默認狀況下從新啓動,但在設置了Activity的configChanges屬性的配置將防止活動被從新啓動,Activity仍在運行而且onConfigurationChanged方法被調用。xml
須要注意的是若是應用程序的target API level是13+的話(聲明瞭minSdkversion和targetSdkVersion屬性),須要同時設置screensize, 由於設備的橫豎方向發生變化的時候,當前屏幕的可用尺寸也將發生變化。