使用WP8最新的AudioVideoCaptureDevice类制作录像应用
WP8出来好一段时间了,新出的AudioVideoCaptureDevice类自定义功能比WP7的CaptureSource强大的多,但网上比较全面的中文实例还比较少,分享一个最近做的小实例给大家参考。
一、实现的功能
1、实现视频和音频的采集并保存
2、允许切换前后摄像头
3、录像过程中可以随时截图
4、录像后视频播放
二、需要的权限
1、需要访问前后摄像头的权限(ID_CAP_ISV_CAMERA)
2、需要访问麦克风的权限(ID_CAP_MICROPHONE)
三 、UI部分
1、使用一个Rectangle控件实时显示摄像头采集到的画面
2、使用一个Image控件来显示视频截图
3、使用一个MediaElement控件来播放录制好的视频
4、使用一个HyperLinkButton控件来切换摄像头
<Canvas x:Name="LayoutRoot" Background="Transparent"> <!--Camera viewfinder >-->
<Rectangle x:Name="Re_video" Width="640" Height="480">
<Rectangle.Fill>
<VideoBrush x:Name="TheVideoBrush"></VideoBrush>
</Rectangle.Fill>
</Rectangle> <MediaElement
x:Name="VideoPlayer"
Width="480"
Height="640"
AutoPlay="True"
RenderTransformOrigin="0.5, 0.5"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Stretch="Fill"
Canvas.Left="0"/> <Image Name="imgCapture" Width="320" Height="240" Margin="20 0 0 0" /> <Grid Margin="20,10" Width="450">
<HyperlinkButton Width="50" x:Name="hyper_changes" Click="hyper_changes_Click" HorizontalAlignment="Right">
<HyperlinkButton.Template>
<ControlTemplate>
<Image Source="/Images/Camera/changes.png" />
</ControlTemplate>
</HyperlinkButton.Template>
</HyperlinkButton>
</Grid>
</Canvas>
四、代码部分
1、定义基础变量
//目录
private string m_path = "Vedio";
//视频文件名
private string m_filename = string.Empty;
//用于设置摄像头旋转的角度
RotateTransform rt1 = new RotateTransform();
//用于设置使用前置还是后置的摄像头
CameraSensorLocation sensorLocation = CameraSensorLocation.Front;
//随机访问数据流
private Windows.Storage.Streams.IRandomAccessStream m_iRandomAccessStream;
//捕获视频设备对象
private Windows.Phone.Media.Capture.AudioVideoCaptureDevice m_captureDevice;
2、初始化录像设备
/// <summary>
/// 初始化捕获设备,
/// </summary>
private async void Init()
{
try
{
if (m_captureDevice != null)
{
m_captureDevice.Dispose();
m_captureDevice = null;
}
//获取视频捕获设备
m_captureDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation, new Windows.Foundation.Size(, )); int sensorOrientation = (Int32)this.m_captureDevice.SensorRotationInDegrees; if (sensorLocation == CameraSensorLocation.Back)
{
rt1.CenterX = ;
rt1.CenterY = ;
rt1.Angle = ;
Re_video.RenderTransform = rt1;
}
else
{
rt1.CenterX = ;
rt1.CenterY = ;
rt1.Angle = ;
Re_video.RenderTransform = rt1;
} m_captureDevice.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, sensorLocation == CameraSensorLocation.Back ? : );
//m_captureDevice.SetProperty(KnownCameraAudioVideoProperties.VideoFrameRate, 20);
m_captureDevice.SetProperty(KnownCameraAudioVideoProperties.UnmuteAudioWhileRecording, true);
m_captureDevice.VideoEncodingFormat = CameraCaptureVideoFormat.H264; TheVideoBrush.SetSource(m_captureDevice);
//设置视频数据格式 }
catch (Exception e)
{
throw e;
}
}
注意:
1)、KnownCameraGeneralProperties.EncodeWithOrientation 可以控制前后摄像头旋转的角度, 默认情况下WP获取的视频都是倒着显示的
2)、KnownCameraAudioVideoProperties.UnmuteAudioWhileRecording 是让视频在录制时取消静音,默认情况下是静音的
3)、CameraCaptureVideoFormat.H264; 使用H264编码方式
3、开始录制视频
//开始录制
private async void StartRecord()
{
m_filename = Guid.NewGuid().ToString() + ".mp4";
try
{
StorageFile file = null;
StorageFolder folder = ApplicationData.Current.LocalFolder;
using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
//不存在目录,则创建
if (!isolatedStorageFile.DirectoryExists(m_path))
{
isolatedStorageFile.CreateDirectory(m_path);
}
} folder = await folder.GetFolderAsync(m_path); m_iRandomAccessStream = null;
//创建视频文件
file = await folder.CreateFileAsync(m_filename, CreationCollisionOption.ReplaceExisting);
m_iRandomAccessStream = await file.OpenAsync(FileAccessMode.ReadWrite);
//开始捕获视频数据并写到随机流中
await m_captureDevice.StartRecordingToStreamAsync(m_iRandomAccessStream); }
catch (Exception ex)
{
throw ex;
}
}
4、录制过程中实现截图功能
/// <summary>
/// 截取视频图像
/// </summary>
private void GetPreview()
{
WriteableBitmap wBitmap = new WriteableBitmap(, );
wBitmap.Render(Re_video, new MatrixTransform());//截取视频
wBitmap.Invalidate();
imgCapture.Source = wBitmap;
}
注意:这段代码的思路其实是截取Rectangle控件的UI界面
5、停止录制视频
//停止录制
private async void StopRecord()
{
try
{
await m_captureDevice.StopRecordingAsync();
m_iRandomAccessStream.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
6、播放录制好的视频
/// <summary>
/// 播放录制的视频
/// </summary>
private void PlayVideo()
{
string path = m_path + "/" + m_filename;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(path, FileMode.Open, FileAccess.Read))
{
VideoPlayer.SetSource(fileStream);
fileStream.Dispose();
fileStream.Close();
}
}
}
7、切换前后摄像头
/// <summary>
/// 切换前后摄像头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void hyper_changes_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (sensorLocation == CameraSensorLocation.Front)
sensorLocation = CameraSensorLocation.Back;
else
sensorLocation = CameraSensorLocation.Front;
Init();
}
至此,大致功能全部实现了, 当然还需要处理一些按钮事件, 这些直接看附件吧:
http://share.weiyun.com/d89188c5490e6bf95c7f7aa08c0f81bc
欢迎有共同兴趣的朋友交流, 微博地址:http://weibo.com/yanghongjin
如果觉得本文对你有帮助,可以微信扫描以下二维码请我喝杯咖啡
使用WP8最新的AudioVideoCaptureDevice类制作录像应用的更多相关文章
- 最新的SqlHelper 类
最新的SqlHelper 类 摘自:http://www.cnblogs.com/sufei/archive/2010/01/14/1648026.html using System; using S ...
- 巧用CSS3 :target 伪类制作Dropdown下拉菜单(无JS)
:target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如说当前页面URL下添加#comment就会定位到id=“comment”的位置,俗称锚).CSS3 为 ...
- 巧用CSS3:target 伪类制作Dropdown下拉菜单(无JS)
原文链接:http://devework.com/css3-target-dropdown.html :target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如 ...
- CSS-用伪类制作小箭头(轮播图的左右切换btn)
先上学习地址:http://www.htmleaf.com/Demo/201610234136.html 作者对轮播图左右按钮的处理方法一改往常,不是简单地用btn.prev+btn.next的图片代 ...
- :target伪类制作tab选项卡
:target伪类的作用是突出显示活动的HTML锚,下面是一个简单的例子: HTML代码: <div> <a href="#demo1">点击此处</ ...
- 使用before、after伪类制作三角形
使用before.after伪类实现三角形的制作,不需要再为三角形增加不必要的DOM元素,影响阅读. <!DOCTYPE html><html><head> ...
- 用css伪类制作三角形的三种方法
在手机上写三角形的时候,我一般都用伪类,刚开始的时候用的图片,但是在现在的手机高清屏幕上,图片容易失真,还是用伪类吧! 第一种:一个90度的“ > ”, 只有线条.(可以做下拉框的箭头之类的) ...
- 利用:before和:after伪类制作CSS3 圆形按钮 含demo
要求 必备知识 基本了解CSS语法,初步了解CSS3语法知识. 开发环境 Adobe Dreamweaver CS6 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 制作步骤: 一 ...
- 利用:before和:after伪类制作类似微信对话框
今天学到了怎么做一个小三角形,进而结合其他属性把类似微信对话框的图形做出来了. 先做出如下形状: .arrow { width: 30px; height:30px; border-width:20p ...
随机推荐
- Msbuild项目集成右键菜单编译
DS1.背景: 我们为什么要将VS2008命令行编译.sln文件集成到右键菜单呢? 原因一:VS2008很好很强大,但太费系统资源了,尤其是在虚拟机在里面装VS2008的时候更是如此. 原因二:有 ...
- 繁星——jquery的data()方法
今天在看JQuery文档的时候偶然看到了data()方法,觉得挺好用的,这里做个记录. 这个方法用于在元素上存放数据,返回jQuery对象.在文档中提到V1.4.3 新增用法NEW data(obj) ...
- Check if a configuration profile is installed on iOS
Configuration profiles can be downloaded to an iOS device through Safari to configure the device in ...
- 【PCB】扫盲总结
1.PCB是什么 PCB( Printed Circuit Board),中文名称为印制电路板,又称印刷线路板,是重要的电子部件,是电子元器件的支撑体,是电子元器件电气连接的载体.由于它是采用电子印刷 ...
- Virtual Box 下Ubuntu桥接网络设置
转自:http://os.51cto.com/art/200908/144564.htm 一般而言,安装完VirtualBox设定网路时选择默认的NAT模式,Guest就可顺利联网了,但是这种方式比较 ...
- Python 五个常用模块资料 os sys time re built-in
1.os模块 os模块包装了不同操作系统的通用接口,使用户在不同操作系统下,可以使用相同的函数接口,返回相同结构的结果. os.name:返回当前操作系统名称('posix', 'nt', ' ...
- Windows Server 2012 中80端口被PID为4的系统进程占用解决方法
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP 把"start" 的值改成4.
- 悟javascript ---------------20160705
1. 首先观察页面需求 如果js要书写多个,那么一定用到循环 或者加上if判断 或者用到switch switch (表达式){ case 值1 : 语句1 break; case 值2 : 语句 ...
- Java JDK 环境变量配置
1:环境 JDK1.7.0 WIN7 2:安装JDK 选择安装目录 安装过程中会出现两次 安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹 ...
- 常用的CSS定位,XPath定位和JPath定位
CSS定位 举例 描述 div#menu id为menu的div元素 div.action-btn.ok-btn class为action-btn和ok-btn的div元素 table#emailLi ...