android 播放視頻示例

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>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章