MainActivity.javapackage com.example.videoviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener,
MediaPlayer.OnErrorListener {
private final String TAG = MainActivity.class.getName();
public VideoView videoView;
public MediaController mediaController;
public int videoPosition = 0;
public ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
dialog.setTitle("视屏播放器");
dialog.setMessage("正在加载...");
dialog.setCancelable(false);
mediaController = new MediaController(this);
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setMediaController(mediaController);
videoView.setOnCompletionListener(this);
videoView.setOnPreparedListener(this);
videoView.setOnErrorListener(this);
}
private void loadVideo() {
Log.d(TAG, "load video");
dialog.show();
try {
//设置视频文件路径
//videoView.setVideoURI(Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.bsg));
videoView.setVideoURI(Uri.parse("file:///sdcard/pxz.mp4"));
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
//开始播放
@Override
protected void onStart() {
Log.d(TAG, "onStart");
super.onStart();
loadVideo();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
public void onCompletion(MediaPlayer mp) {
Log.d(TAG, "Media onCompletion");
Toast.makeText(MainActivity.this, "播放完成", Toast.LENGTH_LONG)
.show();
mp.release();
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Media onPrepared");
if (dialog.isShowing()) {
dialog.dismiss();
}
mp.seekTo(videoPosition);
if (videoPosition == 0) {
mp.start();
} else {
mp.pause();
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d(TAG, "Media onError");
String err = "未知错误";
switch (what) {
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
err = "媒体服务终止";
break;
default:
break;
}
Toast.makeText(MainActivity.this, err, Toast.LENGTH_LONG).show();
return true;
}
}
2 。 activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.videoviewdemo.MainActivity"
android:orientation="vertical">
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"/>
3.为了实现横屏播放需要在AndroidManifest.xml 文件中设置:
android:name=".MainActivity"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
android:configChanges="orientationscreenSizekeyboardHidden"
android:label="@string/app_name" >
说明:视频资源pxz.mp4 放在sdcard 中