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.setMediaController(new MediaController(Activity01.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 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代码
代码片断,双击复制 |
|
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 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> |
源码附件,若是你们要测试须要本身下载一个.mp4文件放入SD卡