当前位置: 移动技术网 > 移动技术>移动开发>Android > android使用videoview播放视频

android使用videoview播放视频

2019年07月24日  | 移动技术网移动技术  | 我要评论

复制代码 代码如下:

public class activity01 extends activity
{
 /** called when the activity is first created. */
 @override
 public void oncreate(bundle savedinstancestate)
 {
  super.oncreate(savedinstancestate);

  setcontentview(r.layout.main);


  final videoview videoview = (videoview) findviewbyid(r.id.videoview01);

  button pausebutton = (button) this.findviewbyid(r.id.pausebutton);
  button loadbutton = (button) this.findviewbyid(r.id.loadbutton);
  button playbutton = (button) this.findviewbyid(r.id.playbutton);

  // load
  loadbutton.setonclicklistener(new onclicklistener() {
   public void onclick(view arg0)
   {
//    videoview.setvideopath("/sdcard/test.mp4");
    videoview.setvideopath("android.resource://com.homer/"+r.raw.china);
    videoview.setmediacontroller(new mediacontroller(activity01.this));
    videoview.requestfocus();
   }
  });

  // play
  playbutton.setonclicklistener(new onclicklistener() {
   public void onclick(view arg0)
   {
    videoview.start();
   }
  });

  // pause
  pausebutton.setonclicklistener(new onclicklistener() {
   public void onclick(view arg0)
   {
    videoview.pause();
   }
  });
 }
}

main.xml

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<absolutelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <textview
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <videoview
        android:id="@+id/videoview01"
        android:layout_width="320px"
        android:layout_height="240px" />

    <button
        android:id="@+id/loadbutton"
        android:layout_width="80px"
        android:layout_height="wrap_content"
        android:layout_x="30px"
        android:layout_y="300px"
        android:text="装载" />

    <button
        android:id="@+id/playbutton"
        android:layout_width="80px"
        android:layout_height="wrap_content"
        android:layout_x="120px"
        android:layout_y="300px"
        android:text="播放" />

    <button
        android:id="@+id/pausebutton"
        android:layout_width="80px"
        android:layout_height="wrap_content"
        android:layout_x="210px"
        android:layout_y="300px"
        android:text="暂停" />

</absolutelayout>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网