MediaElement 可以播放许多不同类型的音频和视频媒体。 MediaElement 是一个可以在其表面显示视频的矩形区域,也可以播放音频。MediaElement 支持触控输入事件。 使用属性 Height 和 Width 可以指定视频显示图面的高度和宽度。为了获得最佳 ...
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>
原标题:windows phone媒体应用开发
关键词:Windows
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。