Android 播放视频示例

Android 播放视频示例 android

 

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

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

  方法                                               说明
  getBufferPercentage                               获得缓冲的百分比
  getCurrentPosition                                获得当前播放的位置
  getDuration                                       获得视频文件的时间
  isPlaying                                         是否正在播放
  pause                                             暂停
  resolveAdjustedSize                               调整视频显示大小
  seekTo                                            指定播放位置
  setMediaController                                设置播放控制器模式(播放进度条)
  setOnCompletionListener                           当媒体文件播放完时触发事件
  setOnErrorListener                                错误监听
  setVideoPath                                      设置视频源路径
  setVideoURI                                       设置视频源地址
  start                                             开始播放
  下面是一个小例子 首先在布局文件中建立VideoView布局,而且建立几个按钮(Button) 来实现对视频的操做,当咱们点击“装载” 按钮时,将指定视频文件的路径,以下代码所示:
  Java代码 算法

? app

代码片断,双击复制 ide

01 布局

02 测试

03 this

04 spa

05 设计

/*设置路径*/ 视频

videoView.setVideoPath"/sdcard/test.mp4");

/*设置模式-播放进度条*/

videoView.setMediaControllernew MediaControllerActivity01.this));

videoView.requestFocus();


  装载以后即可以经过start、pause 方法来播放和暂停,具体代码以下
  Activity01
  Java代码

?

代码片断,双击复制

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

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 onCreateBundle savedInstanceState {

super.onCreatesavedInstanceState);

setContentViewR.layout.main);

/* 建立VideoView对象 */

final VideoView videoView = VideoView findViewByIdR.id.VideoView01);

/* 操做播放的三个按钮 */

Button PauseButton = Button this.findViewByIdR.id.PauseButton);

Button LoadButton = Button this.findViewByIdR.id.LoadButton);

Button PlayButton = Button this.findViewByIdR.id.PlayButton);

/* 装载按钮事件 */

LoadButton.setOnClickListenernew OnClickListener() {

public void onClickView arg0 {

/* 设置路径 */

videoView.setVideoPath"/sdcard/test.mp4");

/* 设置模式-播放进度条 */

videoView.setMediaControllernew MediaController

Activity01.this));

videoView.requestFocus();

}

});

/* 播放按钮事件 */

PlayButton.setOnClickListenernew OnClickListener() {

public void onClickView arg0 {

/* 开始播放 */

videoView.start();

}

});

/* 暂停按钮 */

PauseButton.setOnClickListenernew OnClickListener() {

public void onClickView arg0 {

/* 暂停 */

videoView.pause();

}

});

}

}


  main.xml
  Xml代码

?

代码片断,双击复制

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

xmlnsandroid="http//schemas.android.com/apk/res/android"

androidorientation="vertical"

androidlayout_width="fill_parent"

androidlayout_height="fill_parent"

> 

<TextView

androidlayout_width="fill_parent"

androidlayout_height="wrap_content"

androidtext="@string/hello"

/>

<VideoView

androidid="@+id/VideoView01"

androidlayout_width="320px"

androidlayout_height="240px"

/>

<Button androidid="@+id/LoadButton"

androidlayout_width="80px"

androidlayout_height="wrap_content"

androidtext="装载"

androidlayout_x="30px"

androidlayout_y="300px"

/>

<Button androidid="@+id/PlayButton"

androidlayout_width="80px"

androidlayout_height="wrap_content"

androidtext="播放"

androidlayout_x="120px"

androidlayout_y="300px"

/>

<Button androidid="@+id/PauseButton"

androidlayout_width="80px"

androidlayout_height="wrap_content"

androidtext="暂停"

androidlayout_x="210px"

androidlayout_y="300px"

/>

</AbsoluteLayout>

  源码附件,若是你们要测试须要本身下载一个.mp4文件放入SD卡

相关文章
相关标签/搜索