步骤:
1)布局
将VideoView控件放在一个RelativeLayout中水平居中(防止这部分布局高度随视频大小变化而变化,不好看)
设置一个按钮选择播放的文件,并显示正在播放的文件名
2)选择文件按钮
取得媒体库的所以视频文件信息,保存到列表list,并通过ListView控件和SimperAdapter显示让用户选择
3)VideoView加载文件并播放
其中要VideoView是通过MediaController控制的,要将它们相关联。
4)记录播放进度
这里是考虑,打开其他Activity,屏幕方向改变,home键等情况后,回到播放界面,播放进度丢失,需要了解Activity生命周期,很多博文都写得很清楚。
如果要Activity完全退出(返回键)后仍保存播放进度,应使用SharedPreferences,但这里只考虑上面说到的情况。
效果:
代码:
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.videoview.MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#000000">
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/choose"