- package cn.com.chenzheng_java;
- import android.app.Activity;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.MediaController;
- import android.widget.VideoView;
- import android.widget.MediaController.MediaPlayerControl;
- public class VideoActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.video);
- VideoView videoView = (VideoView)findViewById(R.id.videoView1);
- /***
- * 将播放器关联上一个音频或者视频文件
- * videoView.setVideoURI(Uri uri)
- * videoView.setVideoPath(String path)
- * 以上两个方法都可以。
- */
- videoView.setVideoPath("data/yueding.mp3");
- /**
- * w为其提供一个控制器,控制其暂停、播放……等功能
- */
- videoView.setMediaController(new MediaController(this));
- /**
- * 视频或者音频到结尾时触发的方法
- */
- videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- Log.i("通知", "完成");
- }
- });
- videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
- @Override
- public boolean onError(MediaPlayer mp, int what, int extra) {
- Log.i("通知", "播放中出现错误");
- return false;
- }
- });
- }
- }
video.xml
- <?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="horizontal">
- <VideoView android:layout_height="match_parent" android:id="@+id/videoView1"
- android:layout_width="wrap_content"></VideoView>
- </LinearLayout>
当然,我们也可以播放网络上的多媒体。
我们从api中,可以看到:
setVideoPath(String path) | |
setVideoURI(Uri uri) |