Silverlight使用内建的影音功能来播放,不需要太多影音方面知识,也不需要考虑客户端是否安装Media Player、Quick Time等软件,由于Silverlight的影音功能完全是内置的,支持声音和视频的播放,目前支持的格式如下:
◎ WMA :Windows Media Audio
◎ MP3
◎ WMV1:Windows Media Video 7 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
◎ WMV2:Windows Media Video 8
◎ WMV3:Windows Media Video 9
◎ WMVA
◎ WMVC1
◎ MPEG-4
Silverlight在多媒体影音播放方面具备强大的功能,客户端的计算机安装Silverlight Runtime Component软件,这是最基本的必要条件。
音频和视频功能是集成一个类MediaElement中,媒体元素(MediaElement)继承自UIElent类,继承了UI的通用属性,开发人员可在视频播放的基础上再叠加额外的效果,主要关键属性如下:
◎ AutoPlay:获取或设置是否自动播放Source属性指定的媒体源,默认值为true,表示自动播放。
◎ Balance:获取或设置左右音量平衡比例,属性值为-1到1之间的Double值,默认为0,表示音量相等。
◎ BufferingTime:获取或设置缓冲时间,默认值为5s。
◎ BufferingProgress:获取当前的缓冲进度。
◎ IsMuted:获取或设置媒体源。
◎ Stretch:获取或设置媒体的伸展模式。
◎ Volume:获取或设置播放音量。
下面的文档用于自动下载并播放MP3音频:
<MediaElement x:Name="MyAudio" AutoPlay="True" Volume="1" Source="tt.mp3" Canvas.Left="59" Canvas.Top="102"/>
范例 示范使用摄像头
图9-1是项目启动时画面。
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />
图9-1
◎ 单击“启动”时,会提示是否允许应用程序访问你的本机视频设备,如图9-2。
图9-2
◎ 选择“是”后,开始视频捕获,单击“截屏”下方会出现相应的载图,如图9-3。
图9-3
◎
主要XAML标记如下:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="189*" />
<RowDefinition Height="36*" />
<RowDefinition Height="75*" />
</Grid.RowDefinitions>
<Border x:Name="bordVider" Margin="3" CornerRadius="3" Width="400" BorderBrush="Gray" HorizontalAlignment="Left" BorderThickness="1" >
<Border.Background>
<VideoBrush x:Name="brshMyVideo"/>
</Border.Background>
</Border>
<Button Name="btStart" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1"
Width="78" Height="33"
Content="启动" FontSize="14" Click="btStart_Click" Margin="12,0,0,0" />
<Button Content="关闭" Height="33" HorizontalAlignment="Left" Margin="107,0,0,0" Name="btStop" VerticalAlignment="Top" Width="78" Grid.Row="1" FontSize="14" Click="btStop_Click" />
<Button Content="截屏" Height="33" HorizontalAlignment="Left" Margin="196,0,0,0" Name="btPing" VerticalAlignment="Top" Width="78" Grid.Row="1" FontSize="14" Click="btPing_Click" />
<Image Grid.Row="2" Height="70" HorizontalAlignment="Left" Margin="20,5,0,0" Name="imgMy" Stretch="UniformToFill" VerticalAlignment="Top" Width="99" />
</Grid>
</UserControl>
◎ 主要代码如下:
void btStart_Click(object sender, RoutedEventArgs e)
{
//取得默认视频设备
VideoCaptureDevice video = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
//创建视频捕获源
capSource = new CaptureSource();
if (CaptureDeviceConfiguration.RequestDeviceAccess())
{
//设置视频设备
capSource.VideoCaptureDevice = video;
brshMyVideo.SetSource(capSource);
brshMyVideo.Stretch = Stretch.Fill;
//启动摄像头
capSource.Start();
}
}
private void btStop_Click(object sender, RoutedEventArgs e)
{
//关闭摄像头
capSource.Stop();
}
private void btPing_Click(object sender, RoutedEventArgs e)
{
if (capSource.State == CaptureState.Started)
{
WriteableBitmap wBitmap = new WriteableBitmap(bordVider, new MatrixTransform());
imgMy.Source = wBitmap;
}
}
更详细内容及源代码下载:
转载于:https://blog.51cto.com/wanxl/405214