Android 多媒体应用——VideoView播放视频

来源:互联网 发布:photoshopcs4软件下载 编辑:程序博客网 时间:2024/06/08 20:15

  VideoView是一个用于播放视频的控件,它是属于android .widgt包下的组件。VideoView与ImageView的作用是相同的,只不过是ImageView用于显示图片, 而VideoView用于播放视频。
  

VideoView

  VideoView的使用很简单,一般与MediaController搭配使用。只需要按步骤进行即可。
1. 在布局中定义VieoView的控件。

<LinearLayout 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"    android:orientation="vertical"    tools:context=".MainActivity">    <Button        android:id="@+id/button_start"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="播放视频" />    <VideoView        android:id="@+id/video_show"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

2. 在Activity中获取VideoView控件的对象。
3. 加载视频的来源,加载方式有两种:

  • 直接设置路径: setVideoPath(String path)
  • 设置视频的Uri:
      setVideoURI(Uri uri)
      setVideoURI(Uri uri, Map<String, String> headers)
    4. 与MediaController建立关联。
      setMediaController(MediaController controller)
    5. 调用start()方法开始播放。
public class MainActivity extends Activity {    private Button mButtonStart;    private VideoView mVideoViewShow;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏//         设置横屏显示//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);        setContentView(R.layout.activity_main);        mButtonStart = (Button) findViewById(R.id.button_start);        mVideoViewShow = (VideoView) findViewById(R.id.video_show);        mButtonStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //设置显示的视频的路径                mVideoViewShow.setVideoPath(Environment.getExternalStorageDirectory()+"/xiaoxian.3gp");                //与MediaController关联                mVideoViewShow.setMediaController(new MediaController(MainActivity.this));                //开始播放                mVideoViewShow.start();            }        });    }}

  当然由于我们使用了MediaController, 所以我们可以看到MediaController为我们提供了控制视频的播放/暂停等按钮。如果我们仅仅使用VideoView,而不与MediaController建立关联,那么就只能通过我们自己定义按钮来控制视频的播放和停止了。

0 0
原创粉丝点击