添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
布局:
/VedioViewTest/res/layout/activity_main.xml
<pre name="code" class="html"><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"
>
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
/VedioViewTest/src/com/lxm/vedioviewtest/MainActivity.java
package com.lxm.vedioviewtest;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
VideoView videoView;
MediaController mController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video);
mController = new MediaController(this);
//记得修改路径
String mp4Path = Environment.getExternalStorageDirectory()+"/mv";
File video = new File(mp4Path+"/Roar.mp4");
if(video.exists()){
videoView.setVideoPath(video.getAbsolutePath());
videoView.setMediaController(mController);
// 设置mController与videoView建立关联
mController.setMediaPlayer(videoView);
videoView.requestFocus();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
源码下载