1.制作完的效果
点播放,不能播放,因为网上说是WP7的模拟器不支持音频与视频,必须放在真机上。
2.MainPage.xaml主要部分代码如下:
<!--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}" Height="44"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<MediaElement Height="88" HorizontalAlignment="Left" Name="mediaElementRadio" VerticalAlignment="Top" Width="438" Margin="12,504,0,0" AutoPlay="False"/>
<Button Content="播放" Height="72" HorizontalAlignment="Left" Margin="12,601,0,0" Name="buttonplay" VerticalAlignment="Top" Width="189" Click="buttonplay_Click"/>
</Grid>
</Grid>
3.MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace PhoneMedia
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void buttonplay_Click(object sender, RoutedEventArgs e)
{
mediaElementRadio.Source = new Uri("Sounds/chuangqi.mp3",UriKind.Relative);
mediaElementRadio.Play();
}
}
}
4.设置 Build Action
在solution explorer中点击chuangqi.mp3右键,设置 Build Action 为Content
Build Action 主要有三种:None、Content、Resource;
BuildAction 有下面的几个值:
None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.
Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.
"Resource" : embeds the file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName.g.resources.
Uri有三种类型:
RelativeOrAbsolute:此 Uri 的类型不确定;
Absolute:此 Uri 是绝对 Uri;
Relative:此 Uri 是相对 Uri。
绝对 URI 的特点是完整地引用资源(例如:http://www.contoso.com/index.html),而相对 Uri 取决于前面定义的基 URI(例如:/index.html)。
所以本播放器例子程序Uri用相对的。
5. 另外我把音频文件chuanqi.wav 导入实验,发现不能播放,这应该是格式问题,估计现在还不能播放.wav格式的音频文件
参考:http://msdn.microsoft.com/zh-cn/library/system.urikind.aspx