分类:C#、Android、VS2015;

创建日期:2016-03-11

一、简介

本节例子和上一节的音频播放例子相似,也是最简单的示例,比如并没有考虑视频播放过程中电话打入的情况,也没有考虑复杂的控制。总之,如果你希望进一步学习复杂的例子,首先要先把最核心的简单例子搞明白,否则你连基本的设计思路都不知道,直接看复杂的例子或者实际项目中的代码肯定“事倍功半”,主要原因是你做不到举一反三,只会照搬,稍微让你修改一下功能你就晕了。

实现视频播放的常见方式有:

1、用VideoView和MediaController类实现视频播放

常用,在布局文件中使用VideoView结合MediaController来实现对其控制。本节主要演示它的基本用法。

VideoView控件可以从各种不同的来源(如文件系统、网站)下载视频文件并将视频播放出来。该控件提供了各种控制选项,例如控制视频界面的大小、缩放、着色等。

如果需要更细粒度的控制,也可以用MediaPlayer类来实现。

2、用SurfaceView和MediaPlayer类实现视频播放

这是早期版本提供的办法,实现代码较多。对这种实现有兴趣的可参考其他资料。

二、基本用法示例

1、运行截图

下面的截图是单击【播放RAW下的视频】按钮后看到的视频播放效果。

单击【停止播放】按钮后,可继续单击【播放SD卡Download下的视频】按钮观察,这里就不再截图了。

2、设计步骤

(1)添加视频文件

由于只是为了演示,所以该例子Raw文件夹下和SD卡的Download文件夹下存放的是同一个视频文件(videoviewdemo.mp4)。

至于如何将文件复制到SD卡上,请参看【常见问题解答】。

(2)添加ch2002Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/ch2002_btnRawStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="播放RAW下的视频" />
<Button
android:id="@+id/ch2002_btnSdcardStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="播放SD卡Dwonload下的视频" />
<Button
android:id="@+id/ch2002_btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止播放" />
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ch2002_videoView1"
android:layout_margin="20dp" />
</LinearLayout>

(3)添加ch2002MainActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Android.Net; namespace MyDemos.SrcDemos
{
[Activity(Label = "例20-2 视频播放基本用法")]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { ch.MyDemosCategory })]
public class ch2002MainActivity : Activity
{
VideoView mVideoView;
Button btnRawStart, btnSdcardStart, btnStop; protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.ch2002Main);
mVideoView = FindViewById<VideoView>(Resource.Id.ch2002_videoView1);
btnRawStart = FindViewById<Button>(Resource.Id.ch2002_btnRawStart);
btnSdcardStart = FindViewById<Button>(Resource.Id.ch2002_btnSdcardStart); Uri rawUri = Uri.Parse($"android.resource://{PackageName}/{Resource.Raw.videoviewdemo}");
Uri sdcardUri = Uri.Parse($"{Environment.ExternalStorageDirectory.Name}/Download/videoviewdemo.mp4"); btnRawStart.Click += delegate
{
StartVideo(rawUri);
}; btnSdcardStart.Click += delegate
{
StartVideo(sdcardUri);
}; btnStop = FindViewById<Button>(Resource.Id.ch2002_btnStop);
btnStop.Click += delegate
{
mVideoView.StopPlayback();
ChangePlayingState(true, false);
};
} private void StartVideo(Uri uri)
{
mVideoView.SetVideoURI(uri);
mVideoView.SetMediaController(new MediaController(this));
mVideoView.Start();
ChangePlayingState(false, true);
} private void ChangePlayingState(bool startEnabled,bool stopEnabled)
{
btnRawStart.Enabled = btnSdcardStart.Enabled = startEnabled;
btnStop.Enabled = stopEnabled;
}
}
}

最新文章

  1. 对于Python中self的看法
  2. 黄聪:路由器WIFI连接无法正常访问个别网站及发送图片
  3. html图片和文字的细节
  4. CRM 2013 移动终端 介绍和PAD下载地址
  5. wampserver安装之后连接phpMyAdmin 不成功的解决方法
  6. Entity Framework 插入数据出现重复插入(导航属性硬是要查再一遍???????)
  7. 1141. RSA Attack(RSA)
  8. jQuery实现用户注册的表单验证
  9. python学习第一课要点记录
  10. split 函数自己实现
  11. 为什么每个程序员都应该用Mac OS X?
  12. QNX驱动开发——中断处理(转载)
  13. beanutils中Lazy
  14. HDU 3362 Fix(状压dp)
  15. 会声会影小成果分享(那段青春岁月)——校学习部宣传视频制作&amp;生日祝福
  16. idea 排除编译 参考
  17. BZOJ4668: 冷战 [并查集 按秩合并]
  18. 数学建模:1.概述&amp; 监督学习--回归分析模型
  19. swift中闭包的学习。
  20. asxios--form data提交,setcookie

热门文章

  1. 使用GCD创建单例
  2. HTML5 精灵8方向移动+背景滚动+音效播放+鼠标事件响应
  3. git删除本地的资源,如何恢复?
  4. select的placeholder和分组效果
  5. Hibernate3.x异常No row with the given identifier exists 解决方法
  6. jQuery仿天猫完美加入购物车
  7. (转)CATALINA_BASE与CATALINA_HOME的区别
  8. HTTP 304状态分析
  9. iOS开发-Objective-C Block的实现方式
  10. 【LeetCode】46. Permutations (2 solutions)