现在的位置: 首页 > 综合 > 正文

Android VideoView播放视频

2018年01月18日 ⁄ 综合 ⁄ 共 2830字 ⁄ 字号 评论关闭

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

Android 利用自带VideoView控件播放视频

Activity

  1. public class Activity01 extends Activity  
  2. {  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.   
  9.         setContentView(R.layout.main);  
  10.   
  11.   
  12.         final VideoView videoView = (VideoView) findViewById(R.id.VideoView01);  
  13.       
  14.         Button PauseButton = (Button) this.findViewById(R.id.PauseButton);  
  15.         Button LoadButton = (Button) this.findViewById(R.id.LoadButton);  
  16.         Button PlayButton = (Button) this.findViewById(R.id.PlayButton);  
  17.           
  18.         // load  
  19.         LoadButton.setOnClickListener(new OnClickListener() {  
  20.             public void onClick(View arg0)  
  21.             {  
  22. //              videoView.setVideoPath("/sdcard/test.mp4");  
  23.                 videoView.setVideoPath("android.resource://com.homer/"+R.raw.china);  
  24.                 videoView.setMediaController(new MediaController(Activity01.this));  
  25.                 videoView.requestFocus();  
  26.             }  
  27.         });  
  28.   
  29.         // play  
  30.         PlayButton.setOnClickListener(new OnClickListener() {  
  31.             public void onClick(View arg0)  
  32.             {  
  33.                 videoView.start();  
  34.             }  
  35.         });  
  36.   
  37.         // pause  
  38.         PauseButton.setOnClickListener(new OnClickListener() {  
  39.             public void onClick(View arg0)  
  40.             {  
  41.                 videoView.pause();  
  42.             }  
  43.         });  
  44.     }  
  45. }  

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <VideoView  
  13.         android:id="@+id/VideoView01"  
  14.         android:layout_width="320px"  
  15.         android:layout_height="240px" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/LoadButton"  
  19.         android:layout_width="80px"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_x="30px"  
  22.         android:layout_y="300px"  
  23.         android:text="装载" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/PlayButton"  
  27.         android:layout_width="80px"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_x="120px"  
  30.         android:layout_y="300px"  
  31.         android:text="播放" />  
  32.   
  33.     <Button  
  34.         android:id="@+id/PauseButton"  
  35.         android:layout_width="80px"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_x="210px"  
  38.         android:layout_y="300px"  
  39.         android:text="暂停" />  
  40.   
  41. </AbsoluteLayout>  

运行效果:

源码下载

抱歉!评论已关闭.