[关闭]
@cxm-2016 2016-09-18T02:22:02.000000Z 字数 1164 阅读 2975

Android:使用VideoView播放视频

android no
陈小默(水品有限,恳请批评指正)


VideoView是Android提供的一款用于播放视频的控件[1]


1,添加VideoView组件

我们可以将组件添加到布局文件中,也可以在程序中创建。

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/activity_vv"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.cccxm.crazy.VVActivity">
  7. <VideoView
  8. android:id="@+id/mVideoView"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" />
  11. </FrameLayout>

2,添加视频文件

我们可以采用下面两种方式去添加一个视频文件:

  1. public void setVideoPath(String path);

根据文件路径去播放视频文件

  1. public void setVideoURI(Uri uri);

根据Uri去指定视频文件


3,使用MediaController控制视频播放

VideoView提供了start()stop()pause()三个方法去控制视频播放。但是对于视频播放来说自己去处理逻辑关系仍然稍显复杂。于是Android提供一个MediaController类作为媒体文件播放的控制器。用法如下:

  1. override fun onCreate(savedInstanceState: Bundle?) {
  2. super.onCreate(savedInstanceState)
  3. setContentView(R.layout.activity_vv)
  4. val mController = MediaController(this)
  5. mVideoView.setVideoPath(video)
  6. mVideoView.setMediaController(mController)
  7. mController.setMediaPlayer(mVideoView)
  8. mVideoView.requestFocus()
  9. }


[1] 李刚.疯狂安卓讲义 2th.电子工业出版社.539-540
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注