wp7音乐播放器
前台代码:
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock FontSize="25" x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock FontSize="60" x:Name="PageTitle" Text="播放MediaPlayer" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Width="486" />
</StackPanel>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Height="216" HorizontalAlignment="Left" Margin="35,48,0,0" Name="cover" Stretch="Fill" VerticalAlignment="Top" Width="401" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="1,298,0,0" Name="SongName" Text="" VerticalAlignment="Top" Width="460" />
<Button Content="播放" Height="72" HorizontalAlignment="Left" Margin="6,409,0,0" Name="btn_play_music" VerticalAlignment="Top" Width="444" Click="btn_play_music_Click_1" />
<Button Content="停止" Height="72" HorizontalAlignment="Left" Margin="12,503,0,0" Name="btn_stop" VerticalAlignment="Top" Width="438" Click="btn_stop_Click_1" />
</Grid>
</Grid>
后台代码:
namespace yinyuebofangqi
{
public partial class MainPage : PhoneApplicationPage
{
#region 成员声明
Song _playSong; //声明一个Song 类型的变量
bool _historyItemLaunch;
const String _playSongKey = "playSong";
#endregion
// Constructor
public MainPage()
{
InitializeComponent();
_playSong = null;
_historyItemLaunch = false;
//声明一个Timer 使用线程运行 XNA internals 来自于微软XNA框架
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
//使用委托调用逐帧调用 FrameworkDispatcher.Update();
dt.Tick += delegate
{
try
{
// 这个函数作用是更新各种XNA组件状态,并触发相应的事件,这个是必须的,否则会报异常
FrameworkDispatcher.Update();
}
catch
{
throw;
}
};
dt.Start();
MediaPlayer.MediaStateChanged += new EventHandler<EventArgs>(MediaPlayer_MediaStateChanged);
}
/// <summary>
/// 当页面进入时调用
/// </summary>
/// <param name="e"></param>
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
MediaLibrary library = new MediaLibrary();
if (NavigationContext.QueryString.ContainsKey(_playSongKey))
{
String songToPlay = NavigationContext.QueryString[_playSongKey];
foreach (Song song in library.Songs)
{
if (0 == String.Compare(songToPlay, song.Name))
{
_playSong = song;
}
}
_historyItemLaunch = true;
}
else if (MediaPlayer.State == MediaState.Playing)
{
_playSong = MediaPlayer.Queue.ActiveSong;
}
else
{
Random rd = new Random();
if (library.Songs.Count > 0)
{
_playSong = library.Songs[rd.Next(library.Songs.Count)];
}
else
{
SongName.Text = "no songs in library";
btn_play_music.IsEnabled = false;
}
}
base.OnNavigatedTo(e);
}
void MediaPlayer_MediaStateChanged(object sender, EventArgs e)
{
switch (MediaPlayer.State)
{
case MediaState.Playing:
btn_play_music.IsEnabled = false;
btn_stop.IsEnabled = true;
break;
case MediaState.Paused:
case MediaState.Stopped:
btn_play_music.IsEnabled = true;
btn_stop.IsEnabled = false;
break;
}
}
/// <summary>
/// 为主窗体添加UI加载完成后的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
PopulateSongMetaData();
setInitialButtonState();
if (_historyItemLaunch)
{
playSong();
}
}
/// <summary>
/// 开始播放音乐
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//private void btn_play_music_Click(object sender, RoutedEventArgs e)
//{
// playSong();
//}
///// <summary>
///// 停止播放音乐
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void btn_stop_Click(object sender, RoutedEventArgs e)
//{
// stopSong();
//}
/// <summary>
/// 播放
/// </summary>
private void playSong()
{
if (_playSong != null)
{
MediaPlayer.Play(_playSong);
}
}
/// <summary>
/// 停止
/// </summary>
private void stopSong()
{
if (MediaState.Playing == MediaPlayer.State)
{
MediaPlayer.Stop();
}
}
/// <summary>
/// 使用图片读取图片封面
/// </summary>
private void PopulateSongMetaData()
{
if (_playSong != null)
{
SongName.Text = _playSong.Name;
Stream albumArtStream = _playSong.Album.GetAlbumArt();
if (albumArtStream == null)
{
StreamResourceInfo albumArtPlaceholder = Application.GetResourceStream(new Uri("ApplicationIcon.png", UriKind.Relative));
albumArtStream = albumArtPlaceholder.Stream;
}
BitmapImage albumArtImage = new BitmapImage();
albumArtImage.SetSource(albumArtStream);
cover.Source = albumArtImage;
}
}
/// <summary>
/// 初始化按钮
/// </summary>
private void setInitialButtonState()
{
switch (MediaPlayer.State)
{
case MediaState.Playing:
btn_play_music.IsEnabled = false;
btn_stop.IsEnabled = true;
break;
case MediaState.Paused:
case MediaState.Stopped:
btn_play_music.IsEnabled = true;
btn_stop.IsEnabled = false;
break;
}
}
private void btn_play_music_Click_1(object sender, RoutedEventArgs e)
{
playSong();
}
private void btn_stop_Click_1(object sender, RoutedEventArgs e)
{
stopSong();
}
}
}