view:
public class MyVideoView extends VideoView{
public MyVideoView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
//测量view的可显示区域的大小 这个方法会在view显示之前就会调用
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int width = widthMeasureSpec;
int height = heightMeasureSpec;
Log.i("test", "w="+width+" h="+height);
setMeasuredDimension(width, height);
}
}
public class VideoActivity extends Activity {
VideoView myvideo=null;
String path=Environment.getExternalStorageDirectory()+"/Movies/main.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
myvideo=(VideoView)findViewById(R.id.myvideo);
myvideo.setVideoPath(path);
myvideo.setMediaController(new MediaController(this));
myvideo.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video, menu);
return true;
}
}
xml:
<RelativeLayout 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"
tools:context=".VideoActivity" >
<com.video2.MyVideoView
android:id="@+id/myvideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>