環境:win10,AS 2.3,Vitamio5.2java
這篇說明偏重於先把環境搭建起來,因此文字說明比較少,多輔以圖示但願更直觀的幫助你們。android
一、下載Vitamio最新版本,複製到相應APP project目錄下。網絡
二、AS 添加library到settings.gradleapp
(include ':app', ':vitamio5.2')ide
三、sync的時候會出錯修改library的build.gradle裏面的sdk version和你的phone module的build.gradle同樣。gradle
四、添加depends到你的phone module下的build.greadle裏面ui
五、activity中的主要代碼,這裏的播放路徑能夠本身設置,個人來自於其餘activity的傳遞,各位朋在設置的時候能夠直接賦值例如:movieUrl = 「/mnt/internal_sd/video.mp4」;this
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //必須寫這個,初始化加載庫文件 Vitamio.isInitialized(this); setContentView(R.layout.activity_vitamio_video_view); mVideoView = (VideoView) findViewById(R.id.buffer); intent = getIntent(); movieUrl = intent.getStringExtra("movieUrl"); //判斷path來自於網絡仍是本地 if (!movieUrl.isEmpty()) { if (movieUrl.startsWith("http:")) { mVideoView.setVideoURI(Uri.parse(movieUrl)); } else { mVideoView.setVideoPath(movieUrl); } mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_STRETCH, 0);//全屏 mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);//高畫質 mVideoView.requestFocus(); mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.setPlaybackSpeed(1.0f); } }); } }
六、xml中的代碼code
<?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"> <io.vov.vitamio.widget.CenterLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <io.vov.vitamio.widget.VideoView android:id="@+id/buffer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </io.vov.vitamio.widget.CenterLayout> </RelativeLayout>
七、 manifest中的代碼orm
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 必須初始化 --> <activity android:name="io.vov.vitamio.activity.InitActivity" android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation" android:launchMode="singleTop" android:theme="@android:style/Theme.NoTitleBar" android:windowSoftInputMode="stateAlwaysHidden" /> <!-- 按照官網的說法給播放器所在的Activity設置android:process, 關閉Activity時直接kill,防止底層庫可能存在的BUG致使的崩潰問題--> <activity android:name=".VitamioVideoViewActivity" android:screenOrientation="landscape" android:process=":vitamio" > </activity> </application>
八、關於視頻播放時的花屏的問題,修改Vitamio library中的VideoView,在surfaceCreated添加mSurfaceHolder.setFormat(PixelFormat.RGBX_8888);
public void surfaceCreated(SurfaceHolder holder) { mSurfaceHolder = holder; + mSurfaceHolder.setFormat(PixelFormat.RGBX_8888); if (mMediaPlayer != null && mCurrentState == STATE_SUSPEND && mTargetState == STATE_RESUME) { mMediaPlayer.setDisplay(mSurfaceHolder); resume(); } else { openVideo(); } }