欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android使用videoview播放视频

程序员文章站 2023-10-28 20:22:28
复制代码 代码如下:public class activity01 extends activity{ /** called when the activity...

复制代码 代码如下:

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>