演示如何用android原生的视频浏览器和控制器播放视频。
添加权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
布局:
<?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="match_parent" />
</LinearLayout>
MainActivity:
public class MainActivity extends Activity {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/DCIM/Camera/myvideo.mp4";
Log.i("path",path);
videoView = (VideoView)findViewById(R.id.videoView);
// 本地播放
videoView.setVideoPath(path);
// 网络播放
// videoView.setVideoURI(Uri.parse(""));
// 使用MediaController控制视频播放
MediaController controller = new MediaController(this);
// 设置VideoView与MediaController建立关系
videoView.setMediaController(controller);
// 设置MediaController与videoView的关系
controller.setMediaPlayer(videoView);
}
}