• [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 //分析歌词同步
    2 void analytical(string str)
    3 {
    4
    5 Dictionary<int, string> dicLyc = new Dictionary<int, string>();
    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[i].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[i].Value + Environment.NewLine;
    47 //this.Lrc01Text.Text = " " + lstLycs[i - 2].Value + Environment.NewLine + "→" + lstLycs[i - 1].Value + Environment.NewLine + " " + lstLycs[i].Value;
    48 break;
    49 }
    50 //txtLyc3.Text = lstLycs.Last().Value;
    51 this.Lrc01Text.Text = lstLycs.Last().Value;
    52 }
    53 }





    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>






  • 相关阅读:
    native2ascii转码工具的使用
    文件查找工具Everything小工具的使用
    sql中decode()重要函数使用
    java中随机生成汉字
    java中Random(long seed)方法与rRandom()方法的使用产生随机数
    WEB项目web.xml文件中classpath: 跟classpath*:使用的区别
    170809、 把list集合中的数据按照一定数量分组
    170808、生成为CVS文件
    170807、intellij idea maven集成lombok实例
    170804、使用Joda-Time优雅的处理日期时间
  • 原文地址:https://www.cnblogs.com/tubufeng/p/2404798.html
Copyright © 2020-2023  润新知