wpf和wp7版网络音乐播放器,使用虾米网的资源

http://www.cnblogs.com/wxdtk1989/archive/2011/08/10/2133627.html

软件介绍:本软件使用虾米网的资源,真正下载虾米网的音乐要用积分,所以在这不添加下载功能。代码也只贴部分。我用的NetFramework4.0 不知道在别的电脑上能不能支持。

api是怎么得到的,就是下载它的swf文件,然后反编译,分析代码找到xml地址,MP3地址是加密的,解密算法也是在swf文件里,有兴趣的同学可以研究一下。还有个budge ,mediaElement不在咋了,pause不好使,就是停不下,所以改成了静音效果,伪装成暂停。

下载地址(非源码):https://files.cnblogs.com/wxdtk1989/SkyMusic.zip  不足之处请谅解。

图:

部分代码:

xaml:

  

  

C#:

001 using System;
002 using System.Collections.Generic;
003 using System.Linq;
004 using System.Text;
005 using System.Windows;
006 using System.Windows.Controls;
007 using System.Windows.Data;
008 using System.Windows.Documents;
009 using System.Windows.Input;
010 using System.Windows.Media;
011 using System.Windows.Media.Imaging;
012 using System.Windows.Navigation;
013 using System.Windows.Shapes;
014 using System.Windows.Interop;
015 using System.Runtime.InteropServices;
016 using System.IO;
017 using System.Xml.Linq;
018 using System.Net;
019 using System.Windows.Threading;
020 namespace SkyMuisc
021 {
022     /// <summary>
023     /// MainWindow.xaml 的交互逻辑
024     /// </summary>
025     public partial class MainWindow : Window
026     {
027         public string albumurl;
028         public string[] title1 = new string[80];//歌名
029         public string[] album_name1 = new string[80];//专辑
030         public string[] artist1 = new string[80];//歌手
031         public string[] pic1 = new string[80];//图片
032         public string[] location1 = new string[80];//地址
033         public int x = 0,y=0,z=0,w=0;
034         DispatcherTimer t = new DispatcherTimer();
035         DispatcherTimer t1 = new DispatcherTimer();
036         public MainWindow()
037         {
038             InitializeComponent();
039   
040         }
041         [StructLayout(LayoutKind.Sequential)]
042         public struct MARGINS
043         {
044             public int cxLeftWidth;
045             public int cxRightWidth;
046             public int cyTopHeight;
047             public int cyBottomHeight;
048         }
049         #region  引用 Dll
050         [DllImport("DwmApi.dll")]
051         public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
052         [DllImport("user32.dll", EntryPoint = "SendMessage")]
053         public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
054         [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
055         public static extern int ReleaseCapture();
056         public const int WM_SysCommand = 0x0112;
057         public const int SC_MOVE = 0xF012;
058         [DllImport ("e_xitong.dll")]
059         public static extern void  guanji();
060   
061         #endregion
062         private void Window_Loaded(object sender, RoutedEventArgs e)
063          {
064              try
065              {
066                  StreamReader r = new StreamReader("stye.txt");
067                  string radioche = r.ReadToEnd();
068                  r.Close();
069                  if (radioche=="动漫")
070                  {
071                      radioButton1.IsChecked = true;
072                  }
073                  if (radioche == "华语")
074                  {
075                      radioButton2.IsChecked = true;
076                  }
077                  if (radioche == "欧美")
078                  {
079                      radioButton3.IsChecked = true;
080                  }
081                  if (radioche == "日语")
082                  {
083                      radioButton4.IsChecked = true;
084                  }
085                  if (radioche == "韩语")
086                  {
087                      radioButton5.IsChecked = true;
088                  }
089              }
090              catch (Exception)
091              {
092                  albumurl = albumid("动漫");
093                  radioButton1.IsChecked = true;
094              }
095            // xmlMusic(albumurl);
096             this.Background = Brushes.Transparent;
097             ExtendAeroGlass(this);
098   
099         }
100         private void ExtendAeroGlass(Window window)
101         {
102             try
103             {
104                 // 为WPF程序获取窗口句柄
105                 IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle;
106                 HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
107                 mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
108                 // 设置Margins
109                 MARGINS margins = new MARGINS();
110   
111                 // 扩展Aero Glass
112                 margins.cxLeftWidth = -1;
113                 margins.cxRightWidth = -1;
114                 margins.cyTopHeight = -1;
115                 margins.cyBottomHeight = -1;
116   
117                 int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
118                 if (hr < 0)
119                 {
120                     checkBox1.IsChecked = false;
121                     MessageBox.Show("玻璃效果加载失败!");
122                 }
123             }
124             catch (DllNotFoundException)
125             {
126                 Application.Current.MainWindow.Background = Brushes.White;
127             }
128         }
129         private void Window_MouseDown(object sender, MouseButtonEventArgs e)
130         {
131             IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
132             HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
133             ReleaseCapture();
134             SendMessage(mainWindowSrc.Handle.ToInt32(), WM_SysCommand, SC_MOVE, 0);
135         }
136   
137         private void buttonstart_Click(object sender, RoutedEventArgs e)
138         {
139             w = 0;
140             buttonnext.IsEnabled = true;
141             buttonstart.Visibility = Visibility.Collapsed;
142             buttonpause.Visibility = Visibility.Visible;
143             textBlock4.Visibility = Visibility.Collapsed;
144             if (z == 0)
145             {
146                 information();
147             }
148             mediaElementsong.Volume = 1;
149             t.Interval = new TimeSpan(0,0,1);
150             t.Start();
151             t.Tick += new EventHandler(t_Tick);
152            // mediaElementsong.Play();
153         }
154   
155         void t_Tick(object sender, EventArgs e)
156         {
157             try
158             {
159                 textBlock5.Text = (mediaElementsong.NaturalDuration).ToString().Substring(3, 5);
160                 textBlock6.Text = (mediaElementsong.Position).ToString().Substring(3, 5);
161                 progressBar1.Value =(int)(((double.Parse(textBlock6.Text.Substring(0, 2)) * 60 + double.Parse(textBlock6.Text.Substring(3, 2)))
162                     / (double.Parse(textBlock5.Text.Substring(0, 2)) * 60 + double.Parse(textBlock5.Text.Substring(3, 2)))) * 100);         
163             }
164             catch (Exception)
165             {
166             }
167         }
168         private void buttonpause_Click(object sender, RoutedEventArgs e)
169         {
170             w = 1;
171             buttonstart.Visibility = Visibility.Visible;
172             buttonpause.Visibility = Visibility.Collapsed;
173             mediaElementsong.Volume = 0;  //假暂停
174                //+mediaElementsong.NaturalDuration+'\n'
175                //+mediaElementsong.Position+'\n';
176             z = 1;
177         }
178   
179         private void buttonnext_Click(object sender, RoutedEventArgs e)
180         {
181             buttonstart.Visibility = Visibility.Collapsed;
182             buttonpause.Visibility = Visibility.Visible;
183             w = 0;
184             nextSong();
185             progressBar1.Value = 0;
186             t.Interval = new TimeSpan(0, 0, 1);
187             t.Start();
188             t.Tick += new EventHandler(t_Tick);
189         }
190   
191         private void nextSong()
192         {
193             x++;
194             try
195             {
196                 if (x <= y)
197                 {
198                     information();
199                     textBlock4.Visibility = Visibility.Collapsed;
200                 }
201                 else
202                 {
203                     textBlock4.Visibility = Visibility.Visible;
204                     textBlock4.Text = "加载下一专辑中...";
205                     x = 0;
206                     xmlMusic(albumurl);
207                     information();
208                     textBlock4.Visibility = Visibility.Collapsed;
209                 }
210             }
211             catch (Exception)
212             {
213   
214             }
215         }
216   
217         private void information()
218         {
219             try
220             {
221                 textBlocksing.Text = artist1[x];
222                 textBlocksong.Text = title1[x];
223                 textBlockalbum.Text = album_name1[x];
224                 textBox1.Text = pic1[x];
225                 mediaElementsong.Source = new Uri(location1[x]);
226                 mediaElementsong.Play();
227                 Binding bing = new Binding();
228                 bing.Source = textBox1;
229                 bing.Path = new PropertyPath("Text");
230                 image1.SetBinding(TextBox.TextProperty, bing);
231             }
232             catch (Exception)
233             {
234   
235                 MessageBox.Show("没有地址!!");
236             }
237   
238         }
239   
240         private void buttondown_Click(object sender, RoutedEventArgs e)
241         {
242             try
243             {
244                //
245             }
246             catch (Exception)
247             {
248                 MessageBox.Show("出什么问题了?下不了");
249             }
250         }
251         private string albumid(string some)
252         {
253             string id = "";
254             XDocument xdo = XDocument.Load("albumid/"+some+".xml");
255             var album = (from query in xdo.Elements("album"
256                          select query).FirstOrDefault().Value.Split(']');
257             Random rid = new Random();
258             id=album[rid.Next(0, 200)];
259             return id;
260         }
261   
262         private void radioButton1_Checked(object sender, RoutedEventArgs e)
263         {
264             try
265             {
266                 albumurl = albumid("动漫");
267                 xmlMusic(albumurl);
268                 x = 0;
269                 StreamWriter win = new StreamWriter("stye.txt");
270                 win.Write("动漫");
271                 win.Close();
272             }
273             catch (Exception)
274             {
275   
276             }
277               
278         }
279   
280         private void radioButton2_Checked(object sender, RoutedEventArgs e)
281         {
282             albumurl = albumid("华语");
283             xmlMusic(albumurl);
284             x = 0;
285             StreamWriter win = new StreamWriter("stye.txt");
286             win.Write("华语");
287             win.Close();
288         }
289   
290         private void radioButton3_Checked(object sender, RoutedEventArgs e)
291         {
292             albumurl = albumid("欧美");
293             xmlMusic(albumurl);
294             x = 0;
295             StreamWriter win = new StreamWriter("stye.txt");
296             win.Write("欧美");
297             win.Close();
298         }
299   
300         private void radioButton4_Checked(object sender, RoutedEventArgs e)
301         {
302             albumurl = albumid("日语");
303             xmlMusic(albumurl);
304             x = 0;
305             StreamWriter win = new StreamWriter("stye.txt");
306             win.Write("日语");
307             win.Close();
308         }
309   
310         private void radioButton5_Checked(object sender, RoutedEventArgs e)
311         {
312             albumurl = albumid("韩语");
313             xmlMusic(albumurl);
314             x = 0;
315             StreamWriter win = new StreamWriter("stye.txt");
316             win.Write("韩语");
317             win.Close();
318         }
319         private void xmlMusic(string some)
320         {
321             string httpurl = "http://www.xiami.com。。。。。。";
322             WebClient webc = new WebClient();
323             webc.OpenReadAsync(new Uri(httpurl + some + "/type/3"));
324             webc.OpenReadCompleted += new OpenReadCompletedEventHandler(webc_OpenReadCompleted);
325              
326         }
327         void webc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
328         {
329             int a = 1, b = 1, c = 1, d = 1, e1 = 1, f = 1, g = 1, h = 1, j = 1, k = 1;
330             string webxml;
331             songApi songuri = new songApi();
332             try
333             {
334                 using (StreamReader read = new StreamReader(e.Result, Encoding.UTF8))
335                 {
336                     webxml = read.ReadToEnd();
337                 }
338                 for (int i = 0; i < 60; i++)
339                 {
340                     try
341                     {      //读xml时出问题,我就改成字符串操作了,也挺好的
342                         a = webxml.IndexOf("<title><![CDATA[", a + 16);
343                         b = webxml.IndexOf("]]></title>", b + 11);
344                         title1[i] = webxml.Substring(a + 16, b - a - 16);
345   
346                         c = webxml.IndexOf("<album_name><![CDATA[", c + 21);
347                         d = webxml.IndexOf("]]></album_name>", d + 16);
348                         album_name1[i] = webxml.Substring(c + 21, d - c - 21);
349   
350                         e1 = webxml.IndexOf("<artist>", e1 + 8);
351                         f = webxml.IndexOf("</artist>", f + 9);
352                         artist1[i] = webxml.Substring(e1 + 8, f - e1 - 8);
353   
354                         g = webxml.IndexOf("<location>", g + 10);
355                         h = webxml.IndexOf("</location>", h + 11);
356                         location1[i] = songuri.GetLocation(webxml.Substring(g + 10, h - g - 10));
357   
358                         j = webxml.IndexOf("<pic>", j + 5);
359                         k = webxml.IndexOf("</pic>", k + 6);
360                         string p = webxml.Substring(j + 5, k - j - 5);
361                         pic1[i] = p.Replace("_1", "_2");
362                         y = i;
363                     }
364                     catch (Exception)
365                     {
366                         break;
367                     }
368                 }
369                 textBlock4.Text = "按播放键,播放";
370                 buttonstart.IsEnabled = true;
371                 buttondown.IsEnabled = true;
372             }
373                 
374             catch (Exception)
375             {
376   
377                MessageBox.Show ("貌似网络不行~\\(≧▽≦)//~啦啦啦");
378             }
379         }
380         private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
381         {
382             try
383             {
384                 ImageBrush back = new ImageBrush();
385                 back.ImageSource = new BitmapImage(new Uri("Images/back.jpg", UriKind.Relative));
386                 this.Background = back;
387             }
388             catch (Exception)
389             {
390                 MessageBox.Show("找不到图back.jpg!");
391                 this.Background = Brushes.SkyBlue;
392             }
393   
394   
395         }
396   
397         private void checkBox1_Checked(object sender, RoutedEventArgs e)
398         {
399             this.Background = Brushes.Transparent;
400               
401         }
402   
403         private void mediaElementsong_MediaEnded(object sender, RoutedEventArgs e)
404         {
405             if (w==0)
406             {
407                 nextSong(); 
408             }
409               
410         }
411   
412         private void mediaElementsong_MediaFailed(object sender, ExceptionRoutedEventArgs e)
413         {
414             textBlock4.Visibility = Visibility.Visible;
415             textBlock4.Text = "歌曲加载失败!!";
416         }
417         private void MenuItem2_Click(object sender, RoutedEventArgs e)
418         {
419             this.Close();
420         }
421   
422         private void MenuItem1_Click(object sender, RoutedEventArgs e)
423         {
424             MessageBox.Show("作者:无限的天空1989" + '\n' + "联系方式:gaozhao0331@163.com" +
1 '\n' + "说明:本软件音乐资源使用《虾米网》,纯" + '\n' + "属个人娱乐作品,有不足出错的地方请见谅.");
2         }
3   
4         }
5          
6     }

 

posted @ 2011-08-12 21:55  therockthe  阅读(567)  评论(0)    收藏  举报