在開發中,用到了視頻播放的功能,看到網上現成的開源的Vitamio已經很成熟了。就本着拿來主義直接實用了。
但是播放中實用的進度條的位置有時候跟自己需求不是那么一致。
下面是教程
1.首先修改Vitamio中MediaController類,在此類中新加一個構造函數,代碼如下:
public MediaController(Context context,boolean fromXml,View container) {
super(context);
initController(context);
mFromXml = fromXml;
mRoot = makeControllerView();
//這個地方的FrameLayout.LayoutpParams是因為布局文件中要把MediaController的視圖作為childView加到一個FrameLayout中去
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);//想怎樣布局MediaController就盡情的發揮這個LayoutParams吧
p.gravity = Gravity.BOTTOM;
mRoot.setLayoutParams(p);
((FrameLayout)container).addView(mRoot);
}
此方法中用到的所有字段都是本來就有的,沒有新加任何代碼
2.定義播放視頻的XML布局文件,代碼如下:
<FrameLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="vertical" >
<io.vov.vitamio.widget.CenterLayout
android:id="@+id/cl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<io.vov.vitamio.widget.VideoView
android:id="@+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</io.vov.vitamio.widget.CenterLayout>
</LinearLayout>
</FrameLayout>
3.Activity中使用
/**
*第一個參數:當前上下文
*第二個參數:一定要為TRUE,就是為了在MediaController中執行某些代碼
*第三個參數:把控制器添加到哪個View中去
*/
MediaController mc = new MediaController(this,true,llVideo); vv.setMediaController(mc); mc.setVisibility(View.GONE);//此操作是為了解決打開視頻的時候控制條不走動,需要點擊下視頻才走動的問題。這樣默認情況下用戶看不到進度條,當點擊視頻的時候就可以看到正在走動的進度條了。
希望可以幫到一些人
【玩】
