重新想象 Windows 8 Store Apps (6) - 控件之媒体控件: Image, MediaElement
原文:重新想象 Windows 8 Store Apps (6) - 控件之媒体控件: Image, MediaElement
作者:webabcd
介绍
重新想象 Windows 8 Store Apps 之媒体控件
- Image - 图片控件
- MediaElement - 播放视频或音频的控件
示例
1、Image 的 Demo
ImageDemo.xaml
<Page
x:Class="XamlDemo.Controls.ImageDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <!--
Image - 图片控件
Stretch - 拉伸方式(Windows.UI.Xaml.Media.Stretch 枚举)
Fill - 充满容器,不保留长宽比
None - 不做任何处理,如果图片比容器大,则多出的部分被剪裁
Uniform - 等比缩放到容器(默认值)
UniformToFill - 充满容器,且保留长宽比,多出的部分被剪裁 以下示例图片的原始大小为 150 * 150
-->
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100">
<Image Source="/Assets/Logo.png" Stretch="Fill" Width="200" Height="100" />
</Border> <Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100" Margin="20 0 0 0">
<Image Source="/Assets/Logo.png" Stretch="None" Width="200" Height="100" />
</Border> <Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100" Margin="20 0 0 0">
<Image Source="/Assets/Logo.png" Stretch="Uniform" Width="200" Height="100" />
</Border> <Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100" Margin="20 0 0 0">
<!--后台设置 Image 的 Source-->
<Image Name="myImage" Stretch="UniformToFill" Width="200" Height="100" />
</Border>
</StackPanel> <!--
Image - 图片控件
NineGrid - 指定9网格(相当于flash中的9切片)中的4条线,Thickness 类型
Left - 左边的线相对于图片最左端的距离
Top - 上边的线相对于图片最顶端的距离
Right - 右边的线相对于图片最右端的距离
Bottom - 下边的线相对于图片最底端的距离 以下示例图片的原始大小为 16 * 16
-->
<StackPanel Orientation="Horizontal" Margin="0 50 0 0">
<Image Source="/Assets/NineGrid/Demo.png" Width="200" Height="200" /> <!--通过指定9切片,防止边框被放大或缩小-->
<Image Source="/Assets/NineGrid/Demo.png" Width="200" Height="200" NineGrid="1 1 1 1" Margin="20 0 0 0" />
</StackPanel>
</StackPanel>
</Grid>
</Page>
ImageDemo.xaml.cs
/*
* Image - 图片控件
*/ using System;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging; namespace XamlDemo.Controls
{
public sealed partial class ImageDemo : Page
{
public ImageDemo()
{
this.InitializeComponent(); this.Loaded += ImageDemo_Loaded;
} // 将 Image 控件的图像源设置为 ms-appx:///Assets/Logo.png
private void ImageDemo_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
myImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/Logo.png", UriKind.Absolute)); /*
或者用如下方式指定图象源 RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Logo.png", UriKind.Absolute));
IRandomAccessStream imageStream = await imageStreamRef.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream); myImage.Source = bitmapImage;
*/
}
}
}
2、MediaElement 的 Demo
MediaElementDemo.xaml
<Page
x:Class="XamlDemo.Controls.MediaElementDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Orientation="Horizontal" Margin="120 0 0 0"> <StackPanel>
<Button Name="btnPickFile" Content="播放一个本地视频或音频" Click="btnPickFile_Click_1" /> <Button Name="btnPlayUrl" Content="播放一个网络视频" Click="btnPlayUrl_Click_1" Margin="0 10 0 0" /> <StackPanel Orientation="Horizontal" Margin="0 10 0 0">
<Button x:Name="btnPlay" Content="播放" Margin="0 0 10 0" Click="btnPlay_Click_1" />
<Button x:Name="btnPause" Content="暂停" Margin="0 0 10 0" Click="btnPause_Click_1" />
<Button x:Name="btnStop" Content="停止" Margin="0 0 10 0" Click="btnStop_Click_1" />
</StackPanel> <MediaElement Name="mediaElement" AutoPlay="True" PosterSource="/Assets/Logo.png" Width="480" Height="320" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 10 0 0" />
</StackPanel> <TextBlock Name="lblMsg" FontSize="14.667" Margin="10 0 0 0" /> </StackPanel>
</Grid>
</Page>
MediaElementDemo.xaml.cs
/*
* MediaElement - 播放视频或音频的控件
* 顾名思义的几个事件:DownloadProgressChanged, BufferingProgressChanged, CurrentStateChanged, MediaOpened, MediaEnded, MediaOpened, SeekCompleted, VolumeChanged
* RateChanged - 当 PlaybackRate 的值发生改变时所触发的事件
* 说明:
* 1、正常播放:DefaultPlaybackRate = 1, PlaybackRate = 1
* 2、两倍速快进播放:DefaultPlaybackRate = 0, PlaybackRate = 2
* 2、一倍速快退播放:DefaultPlaybackRate = 0, PlaybackRate = -1
* MarkerReached - 播放过程中遇到时间线标记时所触发的事件
* 说明:通过 MarkerReached 属性来设置或获取时间线标记
*
* CanPlayType(string type) - 获取对指定类型文件的播放支持的可能性
* Play(), Pause(), Stop() - 播放, 暂停, 停止
* SetSource(IRandomAccessStream stream, string mimeType) - 指定需要播放的媒体流
*/ using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls
{
public sealed partial class MediaElementDemo : Page
{
public MediaElementDemo()
{
this.InitializeComponent(); DispatcherTimer dTimer = new DispatcherTimer();
dTimer.Interval = TimeSpan.FromMilliseconds();
dTimer.Tick += dTimer_Tick;
dTimer.Start();
} void dTimer_Tick(object sender, object e)
{
// 视频源的 uri
lblMsg.Text = "Source: " + mediaElement.Source;
lblMsg.Text += Environment.NewLine; // 占位图,即在媒体播放之前显示的图片(与 html5 中的 video 的 poster 作用一样)
lblMsg.Text += "PosterSource: " + mediaElement.PosterSource;
lblMsg.Text += Environment.NewLine; // 当前的播放状态(Windows.UI.Xaml.Media.MediaElementState 枚举):Closed, Opening, Buffering, Playing, Paused, Stopped
lblMsg.Text += "CurrentState: " + mediaElement.CurrentState;
lblMsg.Text += Environment.NewLine; // 指定或获取播放的进度,TimeSpan 类型
lblMsg.Text += "Position: " + mediaElement.Position;
lblMsg.Text += Environment.NewLine; // 下载的进度(0 - 1之间)
lblMsg.Text += "DownloadProgress: " + mediaElement.DownloadProgress;
lblMsg.Text += Environment.NewLine; // 下载起始点与原始视频起始点的偏移量(0 - 1之间)
// 比如一个网络视频时长为 10 分钟,播放过程中手动指定了 Position 想从 5 分钟处开始播,那么:
// 1、此处的视频已被下载了,那么 DownloadProgressOffset 值为 0
// 2、此处的视频尚未被下载,则 MediaElement 会断掉当前连接,然后重连一个 http 以获取从 5 分钟处开始的数据,此时 DownloadProgressOffset 值为 0.5
lblMsg.Text += "DownloadProgressOffset: " + mediaElement.DownloadProgressOffset;
lblMsg.Text += Environment.NewLine; // 缓冲进度(0 - 1之间)
// 1、小于 1 时则媒体在缓冲
// 2、等于 1 时则缓冲区已被填满,可以播放媒体
lblMsg.Text += "BufferingProgress: " + mediaElement.BufferingProgress;
lblMsg.Text += Environment.NewLine; // 默认的播放速率,默认值为 1
lblMsg.Text += "DefaultPlaybackRate: " + mediaElement.DefaultPlaybackRate;
lblMsg.Text += Environment.NewLine; // 当前播放速率,默认值为 1,如果需要将其指定为其它值,需先将 DefaultPlaybackRate 设置为 0
lblMsg.Text += "PlaybackRate: " + mediaElement.PlaybackRate;
lblMsg.Text += Environment.NewLine; // 是否可 seek
lblMsg.Text += "CanSeek: " + mediaElement.CanSeek;
lblMsg.Text += Environment.NewLine; // 是否可 pause
lblMsg.Text += "CanPause: " + mediaElement.CanPause;
lblMsg.Text += Environment.NewLine; // 媒体的时长
lblMsg.Text += "NaturalDuration: " + mediaElement.NaturalDuration;
lblMsg.Text += Environment.NewLine; // 媒体的原始宽
lblMsg.Text += "NaturalVideoWidth: " + mediaElement.NaturalVideoWidth;
lblMsg.Text += Environment.NewLine; // 媒体的原始高
lblMsg.Text += "NaturalVideoHeight: " + mediaElement.NaturalVideoHeight;
lblMsg.Text += Environment.NewLine; // 媒体的宽高比的宽度部分
lblMsg.Text += "AspectRatioWidth: " + mediaElement.AspectRatioWidth;
lblMsg.Text += Environment.NewLine; // 媒体的宽高比的高度部分
lblMsg.Text += "AspectRatioHeight: " + mediaElement.AspectRatioHeight;
lblMsg.Text += Environment.NewLine; // 是否自动播放
lblMsg.Text += "AutoPlay: " + mediaElement.AutoPlay;
lblMsg.Text += Environment.NewLine; // 是否循环播放
lblMsg.Text += "IsLooping: " + mediaElement.IsLooping;
lblMsg.Text += Environment.NewLine; // 音量(0 - 1 之间)
lblMsg.Text += "Volume: " + mediaElement.Volume;
lblMsg.Text += Environment.NewLine; // 左右声道平衡(-1 到 1 之间)
lblMsg.Text += "Balance: " + mediaElement.Balance;
lblMsg.Text += Environment.NewLine; // 是否静音
lblMsg.Text += "IsMuted: " + mediaElement.IsMuted;
lblMsg.Text += Environment.NewLine; // 当前媒体是否是音频:播放的是音频则为 true,播放的是视频则为 false
lblMsg.Text += "IsAudioOnly: " + mediaElement.IsAudioOnly;
lblMsg.Text += Environment.NewLine; // 音频的用途(Windows.UI.Xaml.Media.AudioCategory 枚举)
// 比如:ForegroundOnlyMedia - 播放的是视频或音乐的音频,且只能前台播放;BackgroundCapableMedia - 播放的是视频或音乐的音频,且支持后台播放(关于音频的后台播放,等写到 MediaControl 时再详细说明)
lblMsg.Text += "AudioCategory: " + mediaElement.AudioCategory;
lblMsg.Text += Environment.NewLine; // 音频流的数量(比如:有的视频支持多种配音,那么每一种配音就是一个音频流)
lblMsg.Text += "AudioStreamCount: " + mediaElement.AudioStreamCount;
lblMsg.Text += Environment.NewLine; // 当前播放的音频流,在音频流集合中的索引。注:可以通过 GetAudioStreamLanguage(int? index) 获取指定音频流所对应的语言
lblMsg.Text += "AudioStreamIndex: " + mediaElement.AudioStreamIndex;
lblMsg.Text += Environment.NewLine; // 设备应该如何播放音频
lblMsg.Text += "AudioDeviceType: " + mediaElement.AudioDeviceType;
} // 播放本地视频或音频
private async void btnPickFile_Click_1(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.VideosLibrary; picker.FileTypeFilter.Add(".wmv");
picker.FileTypeFilter.Add(".mp4");
picker.FileTypeFilter.Add(".mp3");
picker.FileTypeFilter.Add(".wma");
picker.FileTypeFilter.Add(".png"); var file = await picker.PickSingleFileAsync();
if (file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
// 指定需要让 MediaElement 播放的媒体流
mediaElement.SetSource(stream, file.ContentType);
}
} // 通过指定一个 url 播放一个网络视频
private void btnPlayUrl_Click_1(object sender, RoutedEventArgs e)
{
mediaElement.Source = new Uri("http://media.w3.org/2010/05/sintel/trailer.mp4", UriKind.Absolute);
} private void btnPlay_Click_1(object sender, RoutedEventArgs e)
{
mediaElement.Play();
} private void btnPause_Click_1(object sender, RoutedEventArgs e)
{
mediaElement.Pause();
} private void btnStop_Click_1(object sender, RoutedEventArgs e)
{
mediaElement.Stop();
}
}
}
OK
[源码下载]
重新想象 Windows 8 Store Apps (6) - 控件之媒体控件: Image, MediaElement的更多相关文章
- 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree
原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...
- 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager
原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...
- 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试
原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...
- 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding
原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...
- 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom
原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...
- 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示
原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...
- 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView
原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...
- 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom
原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...
- 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础
原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...
- 重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid
原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGr ...
随机推荐
- (萌O(∩_∩)O)哈希知识点小结
噶呜~先来了解一下什么是哈希吧? 当我们要在一堆东西中找到想要的那一个东西,我们常常通过比较来找,理想的情况是不经过任何比较,一次就能找到,怎么才能做到这样呢?那就在记录的储存位置和他的关键字之间建立 ...
- Postfix+Amavisd-new+Spamassassin+ClamAV整合安装
1. 安装软件和依赖包 apt-get install amavisd-new spamassassin clamav-daemon mysql-client mysql-server apt-get ...
- 执行Asp.net应用程序在Linux上的3种托管方式
执行Asp.net应用程序在Linux上的3种托管方式 想要执行Asp.net应用程序在Linux上.我们有3种选择: 1.使用Apache作为Webserver.使用mod_mono:http:// ...
- django中mysql数据库设置错误解决方法
刚在django中settings.py进行设置mysql数据库. 当进行执行python manage.py shell命令时会报以下错误: 只需要在settings.py中 DATABASES = ...
- 【BASH】自己主动清理rman脚本备份文件
************************************************************************ ****原文:blog.csdn.net/clark_ ...
- 应届GIS硕士求职经验总结
记录一下作为一个GIS应届毕业生在帝都找工作的历程吧,之后的经历可能丰富多彩,可能萎靡不振,但这一次走过来了就是这一次的.希望以史为鉴,各位客官也能有所收获. 定位 我们这一届的"烟酒生&q ...
- POJ1505&&UVa714 Copying Books(DP)
Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 7109 Accepted: 2221 Descrip ...
- linux下安装QT过程
说QT是linux下主要的图形开发工具一点都不过分,虽然诺基亚公司放弃Meego.遣散了Qt开发团队,但是它的各种商业.企业版本还是的到了很好的保护,linux下的开发工具集里还是经常看到它的身影,毕 ...
- MongoDB在实际项目
MongoDB在实际项目中的使用 MongoDB简介 MongoDB是近些年来流行起来的NoSql的代表,和传统数据库最大的区别是支持文档型数据库.当然,现在的一些数据库通过自定义复合类型,可变长 ...
- Android开发周报:Android L默认加密用户数据
Android开发周报:Android L默认加密用户数据 新闻 <iCloud前车之鉴,Android L默认开启加密功能>:iCloud 艳照风波再起,第二波女星照片流出,大量女星的裸 ...