2012年3月18日星期日

[Windows Phone 7璀璨]北漂1.0在线歌词播放器三.歌词下载

一.歌词下载用到了WebClient,这是一个封装成异步操作的类,在Windows Phone 7中,所以可能影响到UI主线程的操作,都要进行异步处理。

1.1获取歌词真实地址

View Code
 1  string urlSongInfor = "http://box.zhangmen.baidu.com/x?op=12&count=1&title={0}$${1}$$$$";//获取歌曲信息的地址
2 string urlGeCi = "http://box.zhangmen.baidu.com/bdlrc/";//下载歌词的不完全地址
3 /// <summary>
4 /// 获取歌曲的真实LRC地址
5 /// </summary>
6 /// <param name="songName">歌曲名</param>
7 /// <param name="singerName">歌手名</param>
8 public void getSongAdress(string songName, string singerName)
9 {
10 urlSongInfor = String.Format(urlSongInfor, songName, singerName);//url地址
11 WebClientDown(urlSongInfor);
12
13 }
14 /// <summary>
15 /// 此WebClient异步下载歌曲真实地址
16 /// </summary>
17 /// <param name="uri">地址</param>
18 void WebClientDown(string uri)
19 {
20 WebClient webClenet = new WebClient();
21 webClenet.Encoding = new HtmlAgilityPack.Gb2312Encoding();
22 webClenet.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClenet_DownloadStringCompleted);
23 webClenet.DownloadStringAsync(new Uri(uri, UriKind.RelativeOrAbsolute));
24 }
25 /// <summary>
26 /// 获取歌词
27 /// <param name="songName">歌曲名称</param>
28 /// <param name="singerName">演唱人</param>
29 /// </summary>
30 public void getGeiCi(String str)
31 {
32 string matchCount = @"<count>(?<count>\d+)</count>";//匹配找到歌词个数的正则表达式
33 string matchLrcid = @"<lrcid>(?<id>\d+)</lrcid>";//匹配歌词加密文件名的正则表达式
34 int songCount = 0;//找到歌词个数
35 int lrcid = 0;//歌词加密文件名
36 Regex regex = new Regex(matchCount);
37 Match songInfo = regex.Match(str);
38 songCount = Convert.ToInt32(songInfo.Groups["count"].Value);
39 if (songCount == 0)
40 {
41
42 this.Lrc01Text.Text = "没有找到歌词";//搜索到的歌词数为0
43 }
44 regex = new Regex(matchLrcid);
45 MatchCollection matchResult = regex.Matches(str);
46 foreach (Match temp in matchResult)
47 {
48 lrcid = Convert.ToInt32(temp.Groups["id"].ToString());
49 break;
50 }
51 int fileID = lrcid / 100;//计算出加密后的歌词文件名
52 urlGeCi += fileID + "/" + lrcid + ".lrc";
53 WebCilentGeci(urlGeCi);
54
55 }
56 /// <summary>
57 /// 成功获得歌词的真实地址
58 /// </summary>
59 /// <param name="sender"></param>
60 /// <param name="e"></param>
61 void webClenet_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
62 {
63 getGeiCi(e.Result);
64 }


1.2下载歌词和保存歌词到独立存储空间

View Code
 1  //歌曲名字
2 string SongName;
3 //艺术家 用来保存歌词文件到独立储存空间
4 string Artist;
5 DispatcherTimer TimerClock;
6 /// <summary>
7 /// 储存键值对形式的 时间+歌词
8 /// </summary>
9 List<KeyValuePair<int, string>> lstLycs = new List<KeyValuePair<int, string>>();
10
11 /// <summary>
12 /// 此WebClient异步下载歌词
13 /// </summary>
14 /// <param name="uri">地址</param>
15 void WebCilentGeci(string uri)
16 {
17 WebClient webClenetGeci = new WebClient();
18 webClenetGeci.Encoding = new HtmlAgilityPack.Gb2312Encoding();
19 webClenetGeci.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClenetGeci_DownloadStringCompleted);
20 webClenetGeci.DownloadStringAsync(new Uri(uri, UriKind.RelativeOrAbsolute));
21
22 }
23 /// <summary>
24 /// 歌词下载成功
25 /// </summary>
26 /// <param name="sender"></param>
27 /// <param name="e">返回的结果</param>
28 void webClenetGeci_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
29 {
30 //下载成功 进度条循环结束
31 this.DownloadprogressBar.IsIndeterminate = false;
32 //textBlock1.Text = e.Result;
33
34 //歌词文件
35 string str = e.Result;
36 //保存歌词文件到独立空间
37 SaveAndReadlyrics sry = new SaveAndReadlyrics();
38 sry.Save(SongName, Artist,str);
39 analytical(str);
40
41
42 }

1.3分析歌词同步显示

View Code
 1 /// <summary>
2 /// 此WebClient异步下载歌词
3 /// </summary>
4 /// <param name="uri">地址</param>
5 void WebCilentGeci(string uri)
6 {
7 WebClient webClenetGeci = new WebClient();
8 webClenetGeci.Encoding = new HtmlAgilityPack.Gb2312Encoding();
9 webClenetGeci.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClenetGeci_DownloadStringCompleted);
10 webClenetGeci.DownloadStringAsync(new Uri(uri, UriKind.RelativeOrAbsolute));
11
12 }
13 /// <summary>
14 /// 歌词下载成功
15 /// </summary>
16 /// <param name="sender"></param>
17 /// <param name="e">返回的结果</param>
18 void webClenetGeci_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
19 {
20 //下载成功 进度条循环结束
21 this.DownloadprogressBar.IsIndeterminate = false;
22 //textBlock1.Text = e.Result;
23
24 //歌词文件
25 string str = e.Result;
26 //保存歌词文件到独立空间
27 SaveAndReadlyrics sry = new SaveAndReadlyrics();
28 sry.Save(SongName, Artist,str);
29 analytical(str);
30
31
32 }



1.4加载歌曲专辑图片(专辑图片为MP3文件的专辑封面,如果没有的话,载入预定义图片)

View Code
 1 /// <summary>
2 /// 加载专辑图片
3 /// </summary>
4 private void SongImage()
5 {
6 var AlbymArtStream = MediaPlayer.Queue.ActiveSong.Album.GetAlbumArt();
7 if (AlbymArtStream == null)
8 {
9 StreamResourceInfo albumArtPlaceholder = Application.GetResourceStream(new Uri("AlbumArtPlaceholder.png", UriKind.Relative));
10 AlbymArtStream = albumArtPlaceholder.Stream;
11 }
12 BitmapImage image = new BitmapImage();
13 image.SetSource(AlbymArtStream);
14 image1.Source = image;
15
16 }

1.5实时显示歌曲进度(数据绑定)

View Code
1   void TimerClock_Tick(object sender, EventArgs e)
2 {
3 //在这里处理定时器事件
4 progressBar1.Value = MediaPlayer.PlayPosition.Duration().TotalSeconds;
5 this.FirstTextBlock.Text = MediaPlayer.PlayPosition.Duration().ToString().Substring(0, 8);
6 SongImage();
7
8 }

1.6歌词页面打开前,判断内存中是否已有歌词

View Code
 1  //页面载入
2 private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
3 {
4 TimerClock = new System.Windows.Threading.DispatcherTimer();
5 TimerClock.Interval = new TimeSpan(0, 0, 1);
6 TimerClock.Start();
7 TimerClock.Tick += new EventHandler(TimerClock_Tick);
8 //赋值
9 SongName=MediaPlayer.Queue.ActiveSong.Name;
10 Artist = MediaPlayer.Queue.ActiveSong.Artist.ToString();
11 SaveAndReadlyrics sry = new SaveAndReadlyrics();
12 //如果歌词存在 则读取独立储存中的歌词文件
13 if (sry.decide(SongName, Artist))
14 {
15 //下载成功 进度条循环结束
16 this.DownloadprogressBar.IsIndeterminate = false;
17 SaveAndReadlyrics srys = new SaveAndReadlyrics();
18 string lrc = srys.Read(SongName, Artist);
19 analytical(lrc);
20
21 }
22 //否则下载
23 else
24 {
25 getSongAdress(MediaPlayer.Queue.ActiveSong.Name, MediaPlayer.Queue.ActiveSong.Artist.ToString());
26
27 }
28
29 this.LastTextBlock.Text = MediaPlayer.Queue.ActiveSong.Duration.ToString().Substring(0, 8);
30 this.progressBar1.Maximum = MediaPlayer.Queue.ActiveSong.Duration.TotalSeconds;
31 SongImage();
32
33
34 }

1.7页面XAML代码

View Code
 1  <!--ContentPanel - 在此处放置其他内容-->
2 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
3 <TextBlock Height="29" HorizontalAlignment="Left" Margin="29,491,0,0" Name="Lrc01Text" Text="" VerticalAlignment="Top" Width="399" TextWrapping="Wrap"/>
4 <Image Height="250" HorizontalAlignment="Left" Margin="27,63,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="250" />
5 <ProgressBar Height="45" HorizontalAlignment="Left" Margin="26,319,0,0" Name="progressBar1" VerticalAlignment="Top" Width="401" />
6 <ProgressBar Height="46" HorizontalAlignment="Left" Margin="10,10,0,0" Name="DownloadprogressBar" VerticalAlignment="Top" Width="417" IsIndeterminate="True" />
7 <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,370,0,0" Name="FirstTextBlock" Text="" VerticalAlignment="Top" />
8 <TextBlock Height="30" HorizontalAlignment="Left" Margin="344,370,0,0" Name="LastTextBlock" Text="" VerticalAlignment="Top" />
9 <TextBlock Height="29" HorizontalAlignment="Left" Margin="26,530,0,0" Name="Lrc02Text" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="399" Foreground="#FF28A2B6" />
10 <TextBlock Height="29" HorizontalAlignment="Left" Margin="26,565,0,0" Name="Lrc03Text" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="399" />
11 </Grid>






[Windows Phone 7璀璨]北漂1.0在线歌词播放器三.歌词下载

TAG: