设为首页 收藏本站
查看: 903|回复: 0

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

[复制链接]

尚未签到

发表于 2015-5-9 14:33:46 | 显示全部楼层 |阅读模式
  一.歌词下载用到了WebClient,这是一个封装成异步操作的类,在Windows Phone 7中,所以可能影响到UI主线程的操作,都要进行异步处理。
  1.1获取歌词真实地址


DSC0000.gif DSC0001.gif 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  ///
4 /// 获取歌曲的真实LRC地址
5 ///
6 /// 歌曲名
7 /// 歌手名
8         public void getSongAdress(string songName, string singerName)
9         {
10             urlSongInfor = String.Format(urlSongInfor, songName, singerName);//url地址
11             WebClientDown(urlSongInfor);
12            
13         }
14 ///
15 /// 此WebClient异步下载歌曲真实地址
16 ///
17 /// 地址
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 ///
26 /// 获取歌词
27 /// 歌曲名称
28 /// 演唱人
29 ///
30         public void getGeiCi(String str)
31         {
32             string matchCount = @"(?\d+)";//匹配找到歌词个数的正则表达式
33             string matchLrcid = @"(?\d+)";//匹配歌词加密文件名的正则表达式
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         ///
57 /// 成功获得歌词的真实地址
58 ///
59 ///
60 ///
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         ///
7 /// 储存键值对形式的 时间+歌词
8 ///
9         List lstLycs = new List();
10
11 ///
12 /// 此WebClient异步下载歌词
13 ///
14 /// 地址
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         ///
24 /// 歌词下载成功
25 ///
26 ///
27 /// 返回的结果
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 //分析歌词同步
2         void analytical(string str)
3         {
4
5             Dictionary dicLyc = new Dictionary();
6             string _strlyc = str.Replace(Environment.NewLine, "\n");
7             string[] _arrTemp = _strlyc.Split('\n');
8             Regex _reg = new Regex("\\[[0-5][0-9]:[0-5][0-9].[0-9][0-9]\\]");
9             int _key = -1;
10             string _lyc = "";
11             foreach (string item in _arrTemp)
12             {
13                 MatchCollection _mc = _reg.Matches(item);
14                 foreach (Match m in _mc)
15                 {
16                     _key = 1000 * (int.Parse(m.Value.Substring(1, 2)) * 60 + int.Parse(m.Value.Substring(4, 2))) + int.Parse(m.Value.Substring(7, 2));//将时间换成总毫秒数
17                     _lyc = item.Substring(item.LastIndexOf(']') + 1);
18                     if (!dicLyc.ContainsKey(_key) && _lyc.Length > 0)
19                     {
20                         dicLyc.Add(_key, _lyc);
21                     }
22                 }
23
24
25             }
26             dicLyc.Add(1, "Loading...");
27             dicLyc.Add(999999, "the end.");
28
29             lstLycs = dicLyc.OrderBy(c => c.Key).ToList();
30
31             foreach (var item in lstLycs)
32             {
33                 Debug.WriteLine(item.Key + "\t" + item.Value);
34             }
35             CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
36  
37         }
38         void CompositionTarget_Rendering(object sender, EventArgs e)
39         {
40             for (int i = 2; i < lstLycs.Count - 1; i++)
41             {
42                 if (MediaPlayer.PlayPosition.TotalMilliseconds < lstLycs.Key)
43                 {
44                     this.Lrc01Text.Text = " " + lstLycs[i - 2].Value + Environment.NewLine;
45                     this.Lrc02Text.Text = " " + lstLycs[i - 1].Value + Environment.NewLine;
46                     this.Lrc03Text.Text = " " + lstLycs.Value + Environment.NewLine;
47                     //this.Lrc01Text.Text = " " + lstLycs[i - 2].Value + Environment.NewLine + "→" + lstLycs[i - 1].Value + Environment.NewLine + " " + lstLycs.Value;
48                     break;
49                 }
50                 //txtLyc3.Text = lstLycs.Last().Value;
51                 this.Lrc01Text.Text = lstLycs.Last().Value;
52             }
53         }
  

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


View Code


1 ///
2 /// 加载专辑图片
3 ///
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  
2         
3            
4            
5            
6            
7            
8            
9            
10            
11         
  

  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65288-1-1.html 上篇帖子: 浅谈Windows Phone 7的体系结构 下篇帖子: Windows Phone 7 水平滚动的文本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表