MediaElement 可以播放许多不同类型的音频和视频媒体。

  MediaElement 是一个可以在其表面显示视频的矩形区域,也可以播放音频。MediaElement 支持触控输入事件。 使用属性 Height 和 Width 可以指定视频显示图面的高度和宽度。为了获得最佳性能,应避免显式设置 MediaElement 的宽度和高度。而是将这些值保留为默认。 指定源之后,媒体将以其实际大小显示,布局将重新计算该大小。如果需要更改媒体显示的大小,最好使用媒体编码工具将媒体重新编码为所需大小。

  默认情况下,加载 MediaElement 对象后,将立即播放由 Source 属性定义的媒体。若要禁止媒体自动启动,需要将 AutoPlay 属性设置为 false。

  MediaElement 将具有为 0 的 ActualWidth 和 ActualHeight,直到它开始播放。如果您正侦听 MouseLeftButtonDown 事件以触发视频的启动,这可能导致后果。一种简单的解决方法是将 Rectangle 放在 MediaElement 后面,并将鼠标事件与 Rectangle 挂接。然后,将 MediaElement 上的 IsHitTestVisible 属性设置为 false。

使用 MediaElement API 播放视频文件

  使用 MediaElement API 播放本地视频文件的步骤:

    1.在 Visual Studio 中打开一个新的或现有的解决方案。

    2.在“解决方案资源管理器”中,右键单击您项目的名称,单击“添加”,然后单击“现有项”。

    3.导航到现有视频文件并将其添加到项目。在“属性”窗口中,将“生成操作”设置为“内容”。

    4.在 XAML 代码中设置媒体的路径,并设置是否自动播放。

  使用 MediaElement API 播放远程视频文件的步骤:

    1.在 Visual Studio 中打开一个新的或现有的解决方案。

    2.在 XAML 代码中设置媒体的路径,并设置是否自动播放。

    (注意:以上播放文件,视频必须使用支持的 Windows Phone 8 媒体编解码器中列出的编解码器进行编码。)

MediaElement 属性

  MediaElement 对象提供几个媒体特定的属性。下面的列表描述了常用的属性。

    1.AutoPlay:指定 MediaElement 是否应自动开始播放。默认值为 True。

    2.IsMuted:指定 MediaElement 是否静音。

    3.True 值将使 MediaElement 静音。默认值为 false。

    4.Stretch:指定如何拉伸视频以填充 MediaElement 对象。可能的值有 None、Uniform、UniformToFill 和 Fill。默认值为 Fill。

    5.Volume:指定介于 0 到 1 的 MediaElement 对象的音频音量值,1 表示最大音量。默认值为 0.5。

控制媒体播放

  可以使用 MediaElement 对象的 Play、Pause 和 Stop 方法来控制媒体播放。当 MediaElement 对象正在播放时,设备上的所有其他媒体播放将停止。

    下面例子是从网络上下载保存到本地,并进行播放:

      MainPage.xaml主要代码如下:

 <phone:PhoneApplicationPage.Resources>
<ControlTemplate x:Key="PlayButtonControlTemplate" TargetType="Button">
<Grid> <Image x:Name="image" Margin="12,8,13,8" Source="appbar.transport.play.rest.png" RenderTransformOrigin="0.648,0.536">
<Image.RenderTransform>
<CompositeTransform/>
</Image.RenderTransform>
</Image>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="PauseControlTemplate" TargetType="Button">
<Grid>
<Image Margin="8" Source="appbar.transport.pause.rest.png"/>
</Grid>
</ControlTemplate>
</phone:PhoneApplicationPage.Resources> <!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="演示程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="WebClicent下载" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel> <!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Rectangle Height="54" HorizontalAlignment="Left" Margin="39,0,0,83" Name="rectangle1" Stroke="#FFF5F2F2" StrokeThickness="1" VerticalAlignment="Bottom" Width="280" />
<TextBlock Height="54" HorizontalAlignment="Left" Margin="408,470,0,0" Name="TextBlock1" Text="%" VerticalAlignment="Top" FontSize="32" Width="33" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="39,434,0,0" Name="textBlock2" Text="下载进度:" VerticalAlignment="Top" />
<Rectangle Height="54" HorizontalAlignment="Left" Margin="39,470,0,0" Name="DownProgressRectangle" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="160" Fill="#FFFCF2F2" />
<MediaElement Height="278" HorizontalAlignment="Left" Margin="0,6,0,0" Name="mediaElement1" VerticalAlignment="Top" Width="450" />
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="0,344,0,0" Name="PlayButton" VerticalAlignment="Top" Width="160" Template="{StaticResource PlayButtonControlTemplate}" />
<TextBlock FontSize="28" Height="54" HorizontalAlignment="Left" Margin="325,470,0,0" Name="DownProgressTextBlock" Text="0" VerticalAlignment="Top" Width="69" TextAlignment="Right" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="186,366,0,0" Name="MesgTextBlock" Text="" VerticalAlignment="Top" Width="228" />
</Grid>
</Grid>

      MainPage.xaml.cs主要代码如下:

 namespace WebClientDownload
{
public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public static string fileName = "01.wmv"; public MainPage()
{
InitializeComponent();
PlayButton.IsEnabled = false;
mediaElement1.Source = null;
this.DownProgressRectangle.Width = ;
WebClient webclient = new WebClient();
if (webclient.IsBusy)
{
webclient.CancelAsync();
}
webclient.OpenReadCompleted += new OpenReadCompletedEventHandler(webclient_OpenReadCompleted);
webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webclient_DownloadProgressChanged);
//下载资源包
webclient.OpenReadAsync(new Uri("http://mschannel9.vo.msecnd.net/o9/mix/09/wmv/key01.wmv" + fileName, UriKind.RelativeOrAbsolute));
PlayButton.Click += new RoutedEventHandler(PlayButton_Click);
//检测MediaElement播放状态
mediaElement1.CurrentStateChanged += new RoutedEventHandler(mediaElement1_CurrentStateChanged);
}
//播放状态
void mediaElement1_CurrentStateChanged(object sender, RoutedEventArgs e)
{
switch (mediaElement1.CurrentState.ToString())
{
case "Opening":
PlayButton.Template = Resources["PlayButtonControlTemplate"] as ControlTemplate;
MesgTextBlock.Text = "正准备播放";
break;
case "Playing":
PlayButton.Template = Resources["PauseControlTemplate"] as ControlTemplate;
MesgTextBlock.Text = "播放中...";
break;
case "Paused":
PlayButton.Template = Resources["PlayButtonControlTemplate"] as ControlTemplate;
MesgTextBlock.Text = "暂停...";
break;
case "Stopped":
PlayButton.Template = Resources["PlayButtonControlTemplate"] as ControlTemplate;
MesgTextBlock.Text = "已经播放完毕";
break;
}
}
//下载进度
void webclient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.DownProgressRectangle.Width = e.ProgressPercentage * 2.8;
this.DownProgressTextBlock.Text = e.ProgressPercentage.ToString();
}
//下载完成
void webclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (file.FileExists(fileName))
{
file.DeleteFile(fileName); }
else
{
IsolatedStorageFileStream cFileStream = file.CreateFile(fileName);
//关闭文件流
cFileStream.Close(); }
//将文件保存到IsolatedStorage
using (IsolatedStorageFileStream FileStream = file.OpenFile(fileName, FileMode.Create, FileAccess.ReadWrite))
{ byte[] bytesInStream = new byte[e.Result.Length];
e.Result.Read(bytesInStream, , (int)bytesInStream.Length);
FileStream.Write(bytesInStream, , bytesInStream.Length);
FileStream.Flush();
}
}
//将数据流读出
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream FileStream = file.OpenFile(fileName, FileMode.Open, FileAccess.ReadWrite))
{
mediaElement1.SetSource(FileStream);
}
}
PlayButton.IsEnabled = true;
} void PlayButton_Click(object sender, RoutedEventArgs e)
{ if (mediaElement1.CurrentState.ToString() != "Playing")
{ mediaElement1.Play();
}
else
{ mediaElement1.Pause();
}
} }

windows phone媒体应用开发的更多相关文章

  1. 课程上线 -“新手入门 : Windows Phone 8.1 开发”

    经过近1个月的准备和录制,“新手入门 : Windows Phone 8.1 开发”系列课程已经在Microsoft 虚拟学院上线,链接地址为:http://www.microsoftvirtuala ...

  2. Windows Phone 8初学者开发—第11部分:设置SounBoard应用程序

    原文 Windows Phone 8初学者开发—第11部分:设置SounBoard应用程序 原文地址: http://channel9.msdn.com/Series/Windows-Phone-8- ...

  3. Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序

    原文 Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序 原文地址: http://channel9.msdn.com/Series/Windows- ...

  4. Windows下USB磁盘开发系列二:枚举系统中所有USB设备

    上篇 <Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>介绍了很简单的获取系统U盘盘符的办法,现在介绍下如何枚举系统中所有USB设备(不光是U盘). 主要调用的API如下: 1 ...

  5. Windows平台下PHP开发环境的配置

    Windows平台下PHP开发环境的配置 一.基本环境 1.Windows XP 32位 2.Apache 2.2.25,下载地址:http://mirror.bit.edu.cn/apache/ht ...

  6. Windows Phone 8.1 开发技术概览 (Universal APP)

    前一阵真的比较懒 WP8.1 已经出来这么长时间了现在才更新BLOG让大家久等了,今天我先为大家介绍下 WP 8.1的开发框架,什么是微软所推崇的 Universal APP,以及我们要开发 Univ ...

  7. 《深入浅出Windows 10通用应用开发》

        <深入浅出Windows 10通用应用开发>采用Windows 10的SDK进行重新改版,整合了<深入浅出Windows Phone 8.1应用开发>和<深入解析 ...

  8. 2013-6-2 [转载自CSDN]如何入门Windows系统下驱动开发

    [序言]很多人都对驱动开发有兴趣,但往往找不到正确的学习方式.当然这跟驱动开发的本土化资料少有关系.大多学的驱动开发资料都以英文为主,这样让很多驱动初学者很头疼.本人从事驱动开发时间不长也不短,大概 ...

  9. Windows下搭建Git开发环境

    Windows下搭建Git开发环境主要有以下三种方法: 1,VS,vs2013和vs2015中已经集成了git插件了 2,msysGit+TortoiseGit 3,msysGit+SourceTre ...

随机推荐

  1. js 弹出div窗口 可移动 可关闭

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. Light Oj - 1134 Be Efficient

    题目传送门:Be Efficient 题意:输入n和m,然后输入有n个元素的一个序列,问有多少个子序列元素的和能整除m. 思路:求前缀和,利用一个前缀的一个定理求解. 前缀和的一个定理是:每次求的前缀 ...

  3. 【codeforces 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  4. qwb与小数

    qwb与小数 Time Limit: 1 Sec  Memory Limit: 128 MB Description qwb遇到了一个问题:将分数a/b化为小数后,小数点后第n位的数字是多少? 做了那 ...

  5. nyoj_42_一笔画问题_201403181935

    一笔画问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...

  6. K - 贪心 基础

    FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...

  7. Spring MVC 入门(一)

    什么是 Spring MVC 学习某一样东西之前,我们一定要大致知道这个东西是什么,能干什么,为什么要用它. Spring MVC 是一个开源平台,一个基于 Spring 的 MVC 框架,它支持基于 ...

  8. 在Hibernate中使用Memcached作为一个二级分布式缓存

    转自:http://www.blogjava.net/xmatthew/archive/2008/08/20/223293.html   hibernate-memcached--在Hibernate ...

  9. WGS84坐标系图层转火星坐标系(是整个图层转哟,不是转单点坐标)

    开篇唠叨(着急的略过) 大天朝“火星坐标系”小科普:是一种国家保密插件,对真实坐标系统进行人为的加偏处理,将真实的坐标加密成虚假的坐标,加密后的坐标被称为火星坐标系统.所有的电子地图所有的导航设备,都 ...

  10. backup script

    #!/bin/bash ##################################################### # export the whole database use ex ...