原文:重新想象 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的更多相关文章

  1. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  2. 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

    原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...

  3. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  4. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  5. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

  6. 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示

    原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...

  7. 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

  8. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

  9. 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

    原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...

  10. 重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid

    原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGr ...

随机推荐

  1. Mysql ODBC 5.1 Driver免安装脚本

    在使用Mysql 的时候,需要使用ODBC数据源的方式来连接mysql,所以常常需要用到免安装的驱动,自己参考官网的脚本, 修改了一个实用点的脚本,放出来大家一起分享: 安装mysql odbc 5. ...

  2. 用Delphi画圆角Panel的方法(使用CreateRoundRectRgn创造区域,SetWindowRgn显示指定区域)

    用Delphi画圆角Panel的方法: procedure TForm1.Button5Click(Sender: TObject);var fhr :Thandle;beginfhr:=Create ...

  3. URL加随机数的作用

    原文:URL加随机数的作用 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的时候总是使用IE缓存,为了解决这个问题一般可以用一下方法:        1 ...

  4. [转]PHP 5.2~5.6 对照以及功能具体解释

    [分享]PHP 5.2~5.6 对照以及功能具体解释 作者:流水理鱼wwek 来源:http://www.iamle.com/archives/1530.html 截至眼下(2014.2), PHP ...

  5. DELPHI学习---类和对象(五篇)

    Classes and objects(类和对象) 类(或者类类型)定义了一个结构,它包括字段(也称为域).方法和属性:类的实例叫做对象:类的字段.方法和属性被称为它的部件(components)或成 ...

  6. Java 5种字符串拼接方式性能比较。

    最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...

  7. WOJ 1020

    #include<stdio.h> #include<stdlib.h> int main() { int n,m; int *num,*link; int i,j,t,k=0 ...

  8. 基于Qt的类QQ气泡聊天的界面开发

    近期在写IM 聊天界面,想设计出一个类似QQ气泡聊天的样式 使用了几种办法 1:使用Qt以下的QListview来实现QQ类似效果.差强人意 2:使用QWebview载入html css样式来完毕.发 ...

  9. CString的部分实现剖析

    一.CString初探: 在CString的实现中,其最基础的类结构如下: CString其实只有一个数据成员m_pszData,这个成员指向了字符串的首地址.但在MFC的具体实现中, m_pszDa ...

  10. python语言学习9——使用list和tuple

    list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 位置 用索引来访问list中每一个位置的元素,记得索引是从0开始的,到 len-1结 ...