码迷,mamicode.com
首页 > Windows程序 > 详细

windows phone媒体应用开发

时间:2015-10-19 22:13:21      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

  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主要代码如下:

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

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

技术分享
  1 namespace WebClientDownload
  2 {
  3     public partial class MainPage : PhoneApplicationPage
  4     {
  5         // 构造函数
  6           public static string fileName = "01.wmv";
  7        
  8         public MainPage()
  9         {
 10             InitializeComponent();
 11             PlayButton.IsEnabled = false;
 12             mediaElement1.Source = null;
 13             this.DownProgressRectangle.Width = 0;
 14             WebClient webclient = new WebClient();
 15             if (webclient.IsBusy)
 16            {
 17                 webclient.CancelAsync();
 18             }
 19             webclient.OpenReadCompleted += new OpenReadCompletedEventHandler(webclient_OpenReadCompleted);
 20             webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webclient_DownloadProgressChanged);
 21             //下载资源包
 22             webclient.OpenReadAsync(new Uri("http://mschannel9.vo.msecnd.net/o9/mix/09/wmv/key01.wmv" + fileName, UriKind.RelativeOrAbsolute));
 23             PlayButton.Click += new RoutedEventHandler(PlayButton_Click);
 24             //检测MediaElement播放状态
 25             mediaElement1.CurrentStateChanged += new RoutedEventHandler(mediaElement1_CurrentStateChanged);
 26         }
 27         //播放状态
 28         void mediaElement1_CurrentStateChanged(object sender, RoutedEventArgs e)
 29         {
 30             switch (mediaElement1.CurrentState.ToString())
 31             {
 32                 case "Opening":
 33                     PlayButton.Template = Resources["PlayButtonControlTemplate"] as ControlTemplate;
 34                     MesgTextBlock.Text = "正准备播放";
 35                     break;
 36                 case "Playing":
 37                     PlayButton.Template = Resources["PauseControlTemplate"] as ControlTemplate;
 38                     MesgTextBlock.Text = "播放中...";
 39                     break;
 40                 case "Paused":
 41                     PlayButton.Template = Resources["PlayButtonControlTemplate"] as ControlTemplate;
 42                     MesgTextBlock.Text = "暂停...";
 43                     break;
 44                 case "Stopped":
 45                     PlayButton.Template = Resources["PlayButtonControlTemplate"] as ControlTemplate;
 46                     MesgTextBlock.Text = "已经播放完毕";
 47                     break;
 48             }
 49         }
 50         //下载进度
 51         void webclient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
 52         {
 53             this.DownProgressRectangle.Width = e.ProgressPercentage * 2.8;
 54             this.DownProgressTextBlock.Text = e.ProgressPercentage.ToString();
 55         }
 56         //下载完成
 57         void webclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
 58         {
 59             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 60             {
 61                 if (file.FileExists(fileName))
 62                 {
 63                     file.DeleteFile(fileName);
 64 
 65                 }
 66                 else
 67                 {
 68                     IsolatedStorageFileStream cFileStream = file.CreateFile(fileName);
 69                     //关闭文件流
 70                     cFileStream.Close();
 71 
 72                 }
 73                 //将文件保存到IsolatedStorage
 74                 using (IsolatedStorageFileStream FileStream = file.OpenFile(fileName, FileMode.Create, FileAccess.ReadWrite))
 75                 {
 76 
 77                     byte[] bytesInStream = new byte[e.Result.Length];
 78                     e.Result.Read(bytesInStream, 0, (int)bytesInStream.Length);
 79                     FileStream.Write(bytesInStream, 0, bytesInStream.Length);
 80                     FileStream.Flush();
 81                 }
 82             }
 83             //将数据流读出
 84             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 85             {
 86                 using (IsolatedStorageFileStream FileStream = file.OpenFile(fileName, FileMode.Open, FileAccess.ReadWrite))
 87                 {
 88                     mediaElement1.SetSource(FileStream);
 89                 }
 90             }
 91             PlayButton.IsEnabled = true;
 92         }
 93 
 94         void PlayButton_Click(object sender, RoutedEventArgs e)
 95         {
 96 
 97             if (mediaElement1.CurrentState.ToString() != "Playing")
 98             {
 99 
100                 mediaElement1.Play();
101             }
102             else
103             {
104 
105                 mediaElement1.Pause();
106             }
107         }
108 
109     }
View Code


 

windows phone媒体应用开发

标签:

原文地址:http://www.cnblogs.com/spilledlight/p/4893078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!