1.我们新建一个安卓工程,并在res文件夹下建立一个raw的文件夹,把我们要播放的视屏存放至此。废话不多说直接上图。
2.我们对界面进行设计(activity_main.xml)。在这里提一下,刚开始我打的时候,界面成灰色不可用,我在网上下载了jdk7.0以上的版本就行了。好了直接上代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.huchaoxin.FullScreenVideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_gravity="center" />
</RelativeLayout>
3.我们建一个类(FullScreenVideoView.java),用于重写VideoView(从新计算下高度)
package com.example.huchaoxin;
import android.content.Context;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.widget.VideoView;
public class FullScreenVideoView extends VideoView {
public FullScreenVideoView(Context context) {
super(context);
}
public FullScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
setMeasuredDimension(width, height);
}
}
4.修改MainActivity.java文件
package com.example.huchaoxin;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
private MediaPlayer mp = null;
private FullScreenVideoView myVideoView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
initView();
final String videoPath = Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.beijingshiping).toString();
myVideoView.setVideoPath(videoPath);
myVideoView.start();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setLooping(true);
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
myVideoView.setVideoPath(videoPath);
myVideoView.start();
}
});
}
private void initView() {
myVideoView = (FullScreenVideoView) findViewById(R.id.videoView);
}
}