windows phone媒体应用开发
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媒体应用开发的更多相关文章
- 课程上线 -“新手入门 : Windows Phone 8.1 开发”
经过近1个月的准备和录制,“新手入门 : Windows Phone 8.1 开发”系列课程已经在Microsoft 虚拟学院上线,链接地址为:http://www.microsoftvirtuala ...
- Windows Phone 8初学者开发—第11部分:设置SounBoard应用程序
原文 Windows Phone 8初学者开发—第11部分:设置SounBoard应用程序 原文地址: http://channel9.msdn.com/Series/Windows-Phone-8- ...
- Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序
原文 Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序 原文地址: http://channel9.msdn.com/Series/Windows- ...
- Windows下USB磁盘开发系列二:枚举系统中所有USB设备
上篇 <Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>介绍了很简单的获取系统U盘盘符的办法,现在介绍下如何枚举系统中所有USB设备(不光是U盘). 主要调用的API如下: 1 ...
- Windows平台下PHP开发环境的配置
Windows平台下PHP开发环境的配置 一.基本环境 1.Windows XP 32位 2.Apache 2.2.25,下载地址:http://mirror.bit.edu.cn/apache/ht ...
- Windows Phone 8.1 开发技术概览 (Universal APP)
前一阵真的比较懒 WP8.1 已经出来这么长时间了现在才更新BLOG让大家久等了,今天我先为大家介绍下 WP 8.1的开发框架,什么是微软所推崇的 Universal APP,以及我们要开发 Univ ...
- 《深入浅出Windows 10通用应用开发》
<深入浅出Windows 10通用应用开发>采用Windows 10的SDK进行重新改版,整合了<深入浅出Windows Phone 8.1应用开发>和<深入解析 ...
- 2013-6-2 [转载自CSDN]如何入门Windows系统下驱动开发
[序言]很多人都对驱动开发有兴趣,但往往找不到正确的学习方式.当然这跟驱动开发的本土化资料少有关系.大多学的驱动开发资料都以英文为主,这样让很多驱动初学者很头疼.本人从事驱动开发时间不长也不短,大概 ...
- Windows下搭建Git开发环境
Windows下搭建Git开发环境主要有以下三种方法: 1,VS,vs2013和vs2015中已经集成了git插件了 2,msysGit+TortoiseGit 3,msysGit+SourceTre ...
随机推荐
- [SQL Server] 常用sql脚本
1.添加表 GO IF NOT EXISTS(SELECT * FROM sys.tables WHERE name='table_name') BEGIN CREATE TABLE [dbo].[t ...
- 终端打印SQL语句
在 Django 项目的 settings.py 文件中配置: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handle ...
- hdu2011 多项式求和【C++】
多项式求和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Tomcat关闭后,重新启动,session中保存的对象为什么还存在解决方法
Tomcat关闭后,重新启动,session中保存的对象为什么还存在各们朋友大家好: 当我关闭Tomcat,重新启动后,session中保存的对象还依然存在,仍然可以使用,不知这是什么 ...
- ssl_error_weak_server_ephemeral_dh_key
1. http://blog.csdn.net/mmoooodd/article/details/50264559 2. http://blog.csdn.net/zhu19774279/articl ...
- 51nod——1432 独木桥
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1432 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 ...
- hibernate之单表映射
目录 第一章 Hibernate初识 1-1 课程介绍 1-2 什么是ORM 1-3 Hibnerate简介 1-4 开发前的准备 1-5 编写第一个Hibernate例子 1-6 创建hiberna ...
- [Cypress] Test XHR Failure Conditions with Cypress
Testing your application’s behavior when an XHR call results in an error can be difficult. The use o ...
- jQuery EasyUI 1.4更新记录
问题: menu:修复当删除一个menu项时.menu高度不准确. datagrid:修复当datagrid宽度太小时,fitColumns方法不能使用. 改进: 1.为easyui全部组件新增了自适 ...
- js原生offsetParent解析
offsetParent是个仅仅读属性,返回近期显示指定位置的容器元素的引用. 假设元素没有指定位置,近期的元素或者根元素(标准模式下是html,怪异模式下是body)就是offsetParent. ...