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 開始播放
getBufferPercentage 得到緩衝的百分比
getCurrentPosition 得到當前播放的位置
getDuration 得到視頻文件的時間
isPlaying 是否正在播放
pause 暫停
resolveAdjustedSize 調整視頻顯示大小
seekTo 指定播放位置
setMediaController 設置播放控制器模式(播放進度條)
setOnCompletionListener 當媒體文件播放完時觸發事件
setOnErrorListener 錯誤監聽
setVideoPath 設置視頻源路徑
setVideoURI 設置視頻源地址
start 開始播放
下面是一個小例子 首先在佈局文件中創建VideoView佈局,並且創建幾個按鈕(Button) 來實現對視頻的操作,當我們點擊“裝載” 按鈕時,將指定視頻文件的路徑,如下代碼所示:
- /*設置路徑*/
- videoView.setVideoPath("/sdcard/test.mp4");
- /*設置模式-播放進度條*/
- videoView.setMediaController(new MediaController(Activity01.this));
- videoView.requestFocus();
裝載之後便可以通過start、pause 方法來播放和暫停,具體代碼如下
Activity01
- package com.yarin.android.Examples_07_03;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.MediaController;
- import android.widget.VideoView;
- public class Activity01 extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /* 創建VideoView對象 */
- 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);
- /* 裝載按鈕事件 */
- LoadButton.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- /* 設置路徑 */
- videoView.setVideoPath("/sdcard/test.mp4");
- /* 設置模式-播放進度條 */
- videoView.setMediaController(new MediaController(
- Activity01.this));
- videoView.requestFocus();
- }
- });
- /* 播放按鈕事件 */
- PlayButton.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- /* 開始播放 */
- videoView.start();
- }
- });
- /* 暫停按鈕 */
- 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:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <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:text="裝載"
- android:layout_x="30px"
- android:layout_y="300px"
- />
- <Button android:id="@+id/PlayButton"
- android:layout_width="80px"
- android:layout_height="wrap_content"
- android:text="播放"
- android:layout_x="120px"
- android:layout_y="300px"
- />
- <Button android:id="@+id/PauseButton"
- android:layout_width="80px"
- android:layout_height="wrap_content"
- android:text="暫停"
- android:layout_x="210px"
- android:layout_y="300px"
- />
- </AbsoluteLayout>