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类制作录像应用的更多相关文章

  1. 最新的SqlHelper 类

    最新的SqlHelper 类 摘自:http://www.cnblogs.com/sufei/archive/2010/01/14/1648026.html using System; using S ...

  2. 巧用CSS3 :target 伪类制作Dropdown下拉菜单(无JS)

    :target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如说当前页面URL下添加#comment就会定位到id=“comment”的位置,俗称锚).CSS3 为 ...

  3. 巧用CSS3:target 伪类制作Dropdown下拉菜单(无JS)

    原文链接:http://devework.com/css3-target-dropdown.html :target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如 ...

  4. CSS-用伪类制作小箭头(轮播图的左右切换btn)

    先上学习地址:http://www.htmleaf.com/Demo/201610234136.html 作者对轮播图左右按钮的处理方法一改往常,不是简单地用btn.prev+btn.next的图片代 ...

  5. :target伪类制作tab选项卡

    :target伪类的作用是突出显示活动的HTML锚,下面是一个简单的例子: HTML代码: <div> <a href="#demo1">点击此处</ ...

  6. 使用before、after伪类制作三角形

    使用before.after伪类实现三角形的制作,不需要再为三角形增加不必要的DOM元素,影响阅读. <!DOCTYPE html><html><head>    ...

  7. 用css伪类制作三角形的三种方法

    在手机上写三角形的时候,我一般都用伪类,刚开始的时候用的图片,但是在现在的手机高清屏幕上,图片容易失真,还是用伪类吧! 第一种:一个90度的“ > ”, 只有线条.(可以做下拉框的箭头之类的) ...

  8. 利用:before和:after伪类制作CSS3 圆形按钮 含demo

    要求 必备知识 基本了解CSS语法,初步了解CSS3语法知识. 开发环境 Adobe Dreamweaver CS6 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 制作步骤: 一 ...

  9. 利用:before和:after伪类制作类似微信对话框

    今天学到了怎么做一个小三角形,进而结合其他属性把类似微信对话框的图形做出来了. 先做出如下形状: .arrow { width: 30px; height:30px; border-width:20p ...

随机推荐

  1. blade and soul factions

    Faction You will be asked to join one of the elite Factions as a rising Martial Artist no matter wha ...

  2. 14073102(CCDIKRecoil)

    [目标] CCDIKRecoil [思路] 1 CCDIK和Recoil的结合 2 Recoil的回弹机制,逐渐回到原来位置 3 添加一个Recoil基类 [步骤] 1 将\Src\GameFrame ...

  3. 微信公众号支付之坑:调用支付jsapi缺少参数 timeStamp等错误解决方法

    这段时间一直比较忙,一忙起来真感觉自己就只是一台挣钱的机器了(说的好像能挣到多少钱似的,呵呵):这会儿难得有点儿空闲时间,想把前段时间开发微信公众号支付遇到问题及解决方法跟大家分享下,这些“暗坑”能不 ...

  4. Linux x64 下 Matlab R2013a 300 kb 脚本文件调试的 CPU 占用过高问题的解决办法

    (1) 系统+软件版本 CentOS 6.5 (Final), 64 位,内核initramfs-2.6.32-431.5.1.el6.x86_64, MATLAB Version: 8.1.0.60 ...

  5. Oracle查询

    1.普通查询 select * from 表格 查询所有内容 select 列名,列名 from 表格查询某几列 2.条件查询 select * from 表格 where 条件 条件查询 selec ...

  6. bzoj 3196: Tyvj 1730 二逼平衡树

    #include<cstdio> #include<ctime> #include<cstdlib> #include<iostream> #defin ...

  7. 【转】valueof()用法

    valueOf()用来返回对象的原始类型的值. 语法 booleanObject.valueOf() 代码:<script> var a = new String("valueO ...

  8. js生成[n,m]的随机数 以及实际运用

    Math.ceil();  //向上取整. Math.floor();  //向下取整. Math.round();  //四舍五入. Math.random();  //0.0 ~ 1.0 之间的一 ...

  9. Oracle之内存结构(SGA、PGA)

    一.内存结构 SGA(System Global Area):由所有服务进程和后台进程共享: PGA(Program Global Area):由每个服务进程.后台进程专有:每个进程都有一个PGA. ...

  10. python导入cx_Oracle报错的问题!

    import cx_Oracle 总是报错:ImportError: DLL load failed: 找不到指定的模块. 或者:ImportError: DLL load failed: %1 不是 ...