9.1.3 使用VideoView播放

    VideoView是一个带有视频播放功能的视图,可直接在一个布局中使用,因此使用起来非常简单。

    下面是布局XML文件viewthevideo.xml,其在LinearLayout中指定了一个VedioView。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     >
 6  <VedioView 
 7      android:layout_width="wrap_content"
 8      android:layout_height="wrap_content"
 9      android:id="@+id/VedioView"
10   />
11 </LinearLayout>

    为了利用这个VedioView,只须以正常的方式——传入ID(R.id.VedioView)以调用findViewById——获得它的引用。一旦获得了该对象,就可以利用setVideoURI来设置视频文件的URI,然后调用start方法类播放。

package com.nthm.androidtestActivity;

import com.nthm.androidtest.R;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.VideoView;

public class ViewTheVideo extends Activity {
    private VideoView vv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewthevideo);
        vv=(VideoView) findViewById(R.id.VedioView);
        Uri data=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"test.mp4");
        vv.setVideoURI(data);
        vv.start();
    }
}

 

posted on 2014-09-03 11:16  宁静致远,一览众山小  阅读(326)  评论(0编辑  收藏  举报

导航