Winows Phone 7 - 音乐播放示例
openkk
13年前
微软的Microsoft.Xna.Framework.Media组件中自带有MediaPlayer的库,其中库里面存放有三首曲子,我们可以通过引 用该组件来调用库中相应的函数进行歌曲播放,通过引用Microsoft.Xna.Framework.dll,引用该命名空间即可使用该 组件。 <br /> <br /> 这个DEMO是一个音乐播放的小例子,界面如下: <br /> <br /> <img alt="Winows Phone 7 - 音乐播放示例" src="https://simg.open-open.com/show/3d5b989524ea06b4c7e80acfc9a9a8a1.png" width="372" height="698" /> <br /> <br /> 如上图,有四个控件,一个图片控件,一个文本控件,两个控制按钮。当页面被导航进来时,得到一个Song类型的歌曲,并为文本控件赋值歌曲名称。得到类型后,在页面初始完成时,得到一张歌曲的封面并为图片控件赋值。下面是整个应的代码: <br /> <span style="color:#0000ff;"><pre class="brush:c#; toolbar: true; auto-links: false;">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; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Media; using System.Windows.Threading; using System.IO; using System.Windows.Resources; using System.Windows.Media.Imaging;namespace PostCodeManager { 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();+= 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; } } MediaPlayer.MediaStateChanged ; } } </eventargs></pre></span>TIP:OnNavigatedTo方法是一个重写系统事件的方法,此方法以检查导航请求并且准备供显示的页面,为一些必要的组件和处理逻辑提供处理任 务,上一篇提到过工程里面包含有一张图片为SplashScreenImage.jpg,这张图片就是就是为处理方法提供的临时页面。这个做法好处在于, 用户会认为程序启动时和启动后都是一致的,当我们真正程序启动完成 <br /> 后,要做一些页面其他的加载,就可以在pageLoad 里面处理。下面为了证实我所说的,来调试一下吧,首先我们在OnNavigatedTo <br /> 的入口处加一个断点,按F5进入调试模式,我们来看看程序是怎么跑的。如图: <br /> <br /> <img style="width:713px;height:560px;" alt="Winows Phone 7 - 音乐播放示例" src="https://simg.open-open.com/show/e79fb2dbef6e9778048afb9be66a3566.png" /> <br /> <br /> 如图,重写这个方法是不是很不错,在Android 我记得是没有这个事件可以重写的,如果有的话,请麻烦告知一声。谢谢。那么你还在想什么?赶快动手吧,播放一下听听。 <br /> <br /> 源码下载 : <a href="/misc/goto?guid=4959500044155681195" rel="nofollow">猛击点我</a>