android 播放视频示例<转>

来源:互联网 发布:线上抽签软件 编辑:程序博客网 时间:2024/06/06 09:24

http://byandby.iteye.com/blog/845125

由于Android平台由Google自己封装、设计、提供的Java Dalvik 在算法处理效率上无法与C/C++ 或 ARM ASM 相提并论,在描述或移植一些本地语言的解码器上显得无能为力,目前整个平台仅支持MP4 的 H.264、3GP 和 WMV 视频解析。 

   
   Android内置的 VideoView类可以快速制作一个系统播放器,VideoView主要用来显示一个视频文件,我们先开看看VideoView类的一些基本方法。 

    方法                                               说明 
getBufferPercentage                               得到缓冲的百分比 

getCurrentPosition                                得到当前播放的位置 

getDuration                                       得到视频文件的时间 

isPlaying                                         是否正在播放 

pause                                             暂停 

resolveAdjustedSize                               调整视频显示大小 

seekTo                                            指定播放位置 

setMediaController                                设置播放控制器模式(播放进度条)     

setOnCompletionListener                           当媒体文件播放完时触发事件 
     
setOnErrorListener                                错误监听 

setVideoPath                                      设置视频源路径 

setVideoURI                                       设置视频源地址 

start                                             开始播放



  下面是一个小例子 首先在布局文件中创建VideoView布局,并且创建几个按钮(Button) 来实现对视频的操作,当我们点击“装载” 按钮时,将指定视频文件的路径,如下代码所示: 
Java代码  
  1. /*设置路径*/  
  2. videoView.setVideoPath("/sdcard/test.mp4");  
  3. /*设置模式-播放进度条*/  
  4. videoView.setMediaController(new MediaController(Activity01.this));  
  5. videoView.requestFocus();  


  装载之后便可以通过start、pause 方法来播放和暂停,具体代码如下 
Activity01 
Java代码  收藏代码
  1. package com.yarin.android.Examples_07_03;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.MediaController;  
  9. import android.widget.VideoView;  
  10.   
  11. public class Activity01 extends Activity {  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.   
  16.         setContentView(R.layout.main);  
  17.   
  18.         /* 创建VideoView对象 */  
  19.         final VideoView videoView = (VideoView) findViewById(R.id.VideoView01);  
  20.   
  21.         /* 操作播放的三个按钮 */  
  22.         Button PauseButton = (Button) this.findViewById(R.id.PauseButton);  
  23.         Button LoadButton = (Button) this.findViewById(R.id.LoadButton);  
  24.         Button PlayButton = (Button) this.findViewById(R.id.PlayButton);  
  25.   
  26.         /* 装载按钮事件 */  
  27.         LoadButton.setOnClickListener(new OnClickListener() {  
  28.             public void onClick(View arg0) {  
  29.                 /* 设置路径 */  
  30.                 videoView.setVideoPath("/sdcard/test.mp4");  
  31.                 /* 设置模式-播放进度条 */  
  32.                 videoView.setMediaController(new MediaController(  
  33.                         Activity01.this));  
  34.                 videoView.requestFocus();  
  35.             }  
  36.         });  
  37.         /* 播放按钮事件 */  
  38.         PlayButton.setOnClickListener(new OnClickListener() {  
  39.             public void onClick(View arg0) {  
  40.                 /* 开始播放 */  
  41.                 videoView.start();  
  42.             }  
  43.         });  
  44.         /* 暂停按钮 */  
  45.         PauseButton.setOnClickListener(new OnClickListener() {  
  46.             public void onClick(View arg0) {  
  47.                 /* 暂停 */  
  48.                 videoView.pause();  
  49.             }  
  50.         });  
  51.     }  
  52. }  


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

原创粉丝点击