-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadMusic.xaml.cs
290 lines (272 loc) · 10.9 KB
/
DownloadMusic.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml;
using System.Xml.Serialization;
namespace WPF_Player
{
/// <summary>
/// Download.xaml 的交互逻辑
/// </summary>
///
public partial class DownloadMusic : Window
{
DispatcherTimer timer = new DispatcherTimer();//定时器
bool move;
public string text { set; get; }
private BackgroundWorker backgroundWorker = new BackgroundWorker();
private ObservableCollection<Song> songSearchResults = new ObservableCollection<Song>();
private System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
private MyPlayer myPlayer = new MyPlayer();
public DownloadMusic(ref MyPlayer myPlayer)
{
InitializeComponent();
this.myPlayer = myPlayer;
searchText.DataContext = this;
this.listview.DataContext = songSearchResults;
this.backgroundWorker.WorkerSupportsCancellation = true;
this.backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker_DoWork);
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = TimeSpan.FromSeconds(0.1); //设置刷新的间隔时间
timer.Start();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void MiniButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = System.Windows.WindowState.Minimized;
}
private void title_MouseDown(object sender, MouseButtonEventArgs e)
{
move = true;
}
private void title_MouseUp(object sender, MouseButtonEventArgs e)
{
move = false;
}
private void title_MouseMove(object sender, MouseEventArgs e)
{
if (move)
{
try
{
this.DragMove();
}
catch (Exception)
{ }
}
}
private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (backgroundWorker.CancellationPending)
return;
WebClient wb = new WebClient();
byte[] data=null;
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{
data = wb.DownloadData("http://s.music.163.com/search/get/?version=1&src=lofter&type=1&s=" + searchText.Text.Trim() + "&limit=90");
}));
string urlym = Encoding.GetEncoding("utf-8").GetString(data);
if (urlym == "{\"code\":200}")
{
if (songSearchResults.Count == 0)
MessageBox.Show("没有找到匹配的音乐!");
return;
}
else
{
dynamic wyyms = JsonConvert.DeserializeObject(urlym);
dynamic list = wyyms.result.songs;
foreach (dynamic s in list)
{
Song newSong = new Song
{
Location = s.audio.ToString(),
// Name = s.album.name.ToString(),
Name = s.name.ToString(),
Singer = s.artists[0].name.ToString(),
Album= s.album.name.ToString(),
PicUrl=s.album.picUrl.ToString()
};
this.listview.Dispatcher.Invoke(DispatcherPriority.Normal,(Action)(() =>
{
songSearchResults.Add(newSong);
listview.Items.Refresh();
}));
}
}
}
public void downfile(string url, string filename)
{
WebClient web = new WebClient();
web.DownloadFileCompleted += new AsyncCompletedEventHandler(web_DownloadFileCompleted);
web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
web.DownloadFileAsync(new Uri(url), filename);
}
private void DownloadMusicButton_Click(object sender, EventArgs e)
{
if (listview.SelectedItems.Count == 1)
{
Song song = listview.SelectedItem as Song;
string url = "";
//下载歌曲到项目指定文件夹
saveFileDialog.Title = "音乐下载";
saveFileDialog.FileName = song.Name+"-"+song.Singer;
saveFileDialog.Filter = "mp3文件|*.mp3";
if (saveFileDialog.ShowDialog().ToString() == "OK"&&song!=null&&song is Song)
{
downfile(song.Location, saveFileDialog.FileName);
string filename = saveFileDialog.FileName;
string path = filename.Substring(0, filename.LastIndexOf("\\"));
string file = filename.Substring(filename.LastIndexOf("\\") + 1);
string folderPath = ConfigurationManager.AppSettings["Mydownload"];
folderPath = System.IO.Path.GetFullPath(folderPath);
string destPath = folderPath + "\\" + file;
downfile(song.Location, destPath);
url = destPath;
//File.Copy(saveFileDialog.FileName,destPath);
}
//将下载的歌曲写入xml
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ObservableCollection<Song>));
if (!File.Exists("Songs.xml"))
{
ObservableCollection<Song> songs = new ObservableCollection<Song>();
song.Location = url;
songs.Add(song);
using (FileStream fs = new FileStream("Songs.xml", FileMode.Create))
{
xmlSerializer.Serialize(fs, songs);
}
}
else
{
ObservableCollection<Song> songs = new ObservableCollection<Song>();
using (FileStream fs = new FileStream("Songs.xml", FileMode.Open))
{
songs=(ObservableCollection<Song>)xmlSerializer.Deserialize(fs);
song.Location = url;
songs.Add(song);
}
File.Delete("Songs.xml");
using (FileStream fs = new FileStream("Songs.xml", FileMode.Create))
{
xmlSerializer.Serialize(fs, songs);
}
}
}
else if (listview.SelectedItems.Count == 0)
{
MessageBox.Show("请选择一首歌!");
}
else
{
MessageBox.Show("只能选择一首歌下载!");
}
}
int mm = 1;
private void timer_Tick(object sender, EventArgs e)
{
mm++;
}
//显示下载状态
void web_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
this.label.Foreground = new SolidColorBrush(Colors.Red);
this.label.Content = "下载取消";
}
else
{
this.label.Foreground = new SolidColorBrush(Colors.Green);
this.label.Content = "下载完成";
}
timer.Stop();
mm = 0;
}
//显示下载速度
void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (((double)e.BytesReceived / mm / 1024) > 1000)
{
this.label.Content = "下载速度:" + ((double)e.BytesReceived / mm / 1024 / 1024).ToString("F2") + "mb/s";
}
else
{
this.label.Content = "下载速度:" + e.BytesReceived / mm / 1024 + "kb/s";
}
}
private void SearchMusicOnNetwork_Click(object sender, EventArgs e)
{
if (this.searchText.Text == "")
return;
if (backgroundWorker.IsBusy)
{
this.backgroundWorker.CancelAsync();
}
else
{
songSearchResults.Clear();
listview.Items.Refresh();
backgroundWorker.RunWorkerAsync();
}
}
//试听音乐
private void TryToListenSelectedMusic_Click(object sender, EventArgs e)
{
if (listview.SelectedItems.Count == 1)
{
Song song = listview.SelectedItem as Song;
TryToListen tryToListen = new TryToListen();
tryToListen.mp3url = song.Location;
tryToListen.name = song.Name;
tryToListen.singer = song.Singer;
tryToListen.ShowDialog();
}
else if (listview.SelectedItems.Count == 0)
{
MessageBox.Show("请选择一首歌!");
}
else
{
MessageBox.Show("只能选择一首歌试听!");
}
}
//按回车键也可以开始搜索
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyStates == Keyboard.GetKeyStates(Key.Enter))
{
if (backgroundWorker.IsBusy)
{
this.backgroundWorker.CancelAsync();
}
SearchMusicOnNetwork_Click(sender, new EventArgs());
}
}
}
}