好久没写了,一直在搞我的offlet ,这是一个模仿DirectShow架构的.NET实现。音频部分已无大问题,只是如果用DSound作renderer的话在某些笔记本上会出现性能问题,还有音视频同步方案也让人头疼。好了不多说啦,等搞完我会跟大家分享。
我用的是vs2010 和 silverlight 4.0,silverlight 3也具备我们需要的功能和类型库。
在4的类型库中,路径为:C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\System.Windows.dll,我们需要System.Windows.Media命名空间中的 VideoBrush、CaptureSource、VideoCaptureDevice 、AudioCaptureDevice和CaptureDeviceConfiguration 类型。
System.Windows.Media :该命名空间名字很直观,说明里边包含管理多媒体的类库。
VideoBrush: 用视频内容绘画。
CaptureSource:提供方法从分配捕获设备捕获的音频与视频
VideoCaptureDevice :从 System.Windows.Media.CaptureDevice继承。为视频捕获设备描述期望的和支持的视频信息,如WEB摄像头。
AudioCaptureDevice :从 System.Windows.Media.CaptureDevice继承。为音频捕获设备要么描述期望的要么是支持的音频信息,如麦克风。
CaptureDeviceConfiguration :帮助器类型,包含捕获设备信息并处理用户权限。
好了,我们需要的类库已准备好,创建一个Silverlight工程吧。
<Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" />
<Button Content="Stop" Height="23" HorizontalAlignment="Left" Margin="106,8,0,0" Name="btnStop" VerticalAlignment="Top" Width="75" />
<Rectangle x:Name="WebcamCapture" Width="320" Height="240" Fill="White"/>
</Grid>
这是应用到我们工程的XAML代码,一个开始按钮,一个结束按钮,一个绘画区。
Inherits UserControl
Private _captureSource As CaptureSource
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStart.Click
_captureSource = New CaptureSource
'为捕获源设置捕获设备,这里我使用默认设备
_captureSource.VideoCaptureDevice = CType(CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices(0), VideoCaptureDevice)
_captureSource.AudioCaptureDevice = CType(CaptureDeviceConfiguration.GetAvailableAudioCaptureDevices()(0), AudioCaptureDevice)
'创建画刷
Dim vidBrush As New VideoBrush()
vidBrush.SetSource(_captureSource)
WebcamCapture.Fill = vidBrush ' 把画刷设置到绘画区域内
' 处理用户权限并捕获
If CaptureDeviceConfiguration.AllowedDeviceAccess OrElse CaptureDeviceConfiguration.RequestDeviceAccess() Then
_captureSource.Start()
End If
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStop.Click
_captureSource.Stop()
End Sub
End Class
{
Private CaptureSource _captureSource;
public MainPage()
{
InitializeComponent();
}
private void btnStart_Click(object sender,System.Windows.RoutedEventArgs e)
{
_captureSource = new CaptureSource;
//为捕获源设置捕获设备,这里我使用默认设备
_captureSource.VideoCaptureDevice=(VideoCaptureDevice) CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices[0];
_captureSource.AudioCaptureDevice=(AudioCaptureDevice)CaptureDeviceConfiguration.GetAvailableAudioCaptureDevices()[0];
//创建画刷
VideoBrush vidBrush = new VideoBrush();
vidBrush.SetSource(_captureSource);
WebcamCapture.Fill = vidBrush ;// 把画刷设置到绘画区域内
//处理用户权限并捕获
if(CaptureDeviceConfiguration.AllowedDeviceAccess||CaptureDeviceConfiguration.RequestDeviceAccess())
_captureSource.Start();
}
private void btnStart_Click(object sender,System.Windows.RoutedEventArgs e)
{
_captureSource.Stop()
}
}
这样就OK了