android之视频播放控件VideoView简单应用

来源:互联网 发布:天气预报数据库 编辑:程序博客网 时间:2024/06/14 18:26

[java:nogutter] view plaincopyprint?
  1. package cn.com.chenzheng_java;
  2. import android.app.Activity;
  3. import android.media.MediaPlayer;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.widget.MediaController;
  7. import android.widget.VideoView;
  8. import android.widget.MediaController.MediaPlayerControl;
  9. public class VideoActivityextends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.video);
  14. VideoView videoView = (VideoView)findViewById(R.id.videoView1);
  15. /***
  16. * 将播放器关联上一个音频或者视频文件
  17. * videoView.setVideoURI(Uri uri)
  18. * videoView.setVideoPath(String path)
  19. * 以上两个方法都可以。
  20. */
  21. videoView.setVideoPath("data/yueding.mp3");
  22. /**
  23. * w为其提供一个控制器,控制其暂停、播放……等功能
  24. */
  25. videoView.setMediaController(new MediaController(this));
  26. /**
  27. * 视频或者音频到结尾时触发的方法
  28. */
  29. videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  30. @Override
  31. public void onCompletion(MediaPlayer mp) {
  32. Log.i("通知", "完成");
  33. }
  34. });
  35. videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
  36. @Override
  37. public boolean onError(MediaPlayer mp,int what, int extra) {
  38. Log.i("通知", "播放中出现错误");
  39. return false;
  40. }
  41. });
  42. }
  43. }

video.xml

[xhtml:nogutter] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"android:layout_height="match_parent"
  4. android:orientation="horizontal">
  5. <VideoViewandroid:layout_height="match_parent"android:id="@+id/videoView1"
  6. android:layout_width="wrap_content"></VideoView>
  7. </LinearLayout>

当然,我们也可以播放网络上的多媒体。

我们从api中,可以看到:

 setVideoPath(String path) 

setVideoURI(Uri uri)