Android初级开发(四)——补充1、Video View的应用

来源:互联网 发布:淘宝ipad版怎么找相似 编辑:程序博客网 时间:2024/06/05 04:33

             今天在某大神的课程里学到了VideoView的使用,赶紧趁热打铁的来练练手。。。  

    步骤:1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件

          2、调用VideoView的如下两个方法来加载指定的视频

                setVideoPath(String path):加载path文件代表的视频(本地文件中视频)

                setVideoURI(Uri  uri):加载uri所对应的视频

          3、调用VideoView的start()、stop()、pause()方法来控制视频的播放

  

    播放本地视频练习:

    1、路径

    videoview.setVideoURI(Uri.parse("android.resourse;//"+getPackageName()+"/"+R.raw.media));

    2、播放

    videoview.start();

    3、监听

    setOnCompletionListener


    布局文件代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <VideoView        android:id="@+id/video"        android:layout_width="match_parent"        android:layout_height="400dp" />    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/start"            android:layout_weight="1"            android:layout_width="0dp"            android:layout_height="50dp"            android:textSize="30dp"            android:gravity="center"            android:text="开始"/>        <Button            android:id="@+id/stop"            android:layout_weight="1"            android:layout_width="0dp"            android:layout_height="50dp"            android:textSize="30dp"            android:gravity="center"            android:text="停止"/>        <Button            android:id="@+id/pause"            android:layout_weight="1"            android:layout_width="0dp"            android:layout_height="50dp"            android:textSize="30dp"            android:gravity="center"            android:text="暂停"/>    </LinearLayout></LinearLayout>
    Activity中的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    //视频    private VideoView video;    //开始  停止  暂停    private Button start,stop,pause;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    //初始化view    private void initView() {        video = (VideoView) findViewById(R.id.video);        start = (Button) findViewById(R.id.start);        start.setOnClickListener(this);        stop = (Button) findViewById(R.id.stop);        stop.setOnClickListener(this);        pause = (Button) findViewById(R.id.pause);        pause.setOnClickListener(this);        //地址        video.setVideoURI(Uri.parse("android.resourse;//"+getPackageName()+"/"+R.raw.video));        //播放        video.start();        //监听视频的状态        video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {            //结束            @Override            public void onCompletion(MediaPlayer mp) {                video.start();  //无限循环播放的效果            }        });    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.start:                video.start();                break;            case R.id.stop:                video.stopPlayback();                break;            case R.id.pause:                video.pause();                break;        }    }}


            效果图如下:

    

    然而,我的模拟器上显示视频无法播放....好尴尬,谁的能显示?!!



业精于勤荒于嬉,行成于思毁于随


阅读全文
0 0
原创粉丝点击