PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/winbird v0.0.1.2/Audio.cs

#
C# | 660 lines | 546 code | 109 blank | 5 comment | 73 complexity | e87a6ddfeecd3645f9b94b9ea2cb50aa MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Threading;
  7. using System.Drawing;
  8. using System.Net;
  9. using System.ComponentModel;
  10. using System.IO;
  11. using XPTable;
  12. using XPTable.Editors;
  13. using XPTable.Events;
  14. using XPTable.Models;
  15. using XPTable.Renderers;
  16. using XPTable.Sorting;
  17. using Un4seen.Bass;
  18. namespace WinBird
  19. {
  20. public partial class MainForm
  21. {
  22. Thread threadAudio;
  23. Color shrink = System.Drawing.Color.FromArgb(((int)(((byte)(25)))), ((int)(((byte)(35)))), ((int)(((byte)(44)))));
  24. bool manualStop = false;
  25. private void Audio()
  26. {
  27. threadAudio = new Thread(ThreadAudio);
  28. threadAudio.Priority = ThreadPriority.Normal;
  29. threadAudio.Start();
  30. }
  31. private void ThreadAudio()
  32. {
  33. this.listFriends.Invoke(new delegateAudio(this.getAudio), new object[] { api.UserID, 0 });
  34. this.listFriends.Invoke(new delegateGetAlbums(this.getAlbums), new object[] { api.UserID });
  35. }
  36. public delegate void delegateAudio(int uid = 0, int audio_id = 0);
  37. public delegate void delegateGetAlbums(int uid = 0);
  38. private void getAudio(int uid = 0, int album_id = 0)
  39. {
  40. this.listAudio.TableModel.Rows.Clear();
  41. this.searchTrue = false;
  42. try
  43. {
  44. string[,] arr = new string[,] { { "uid", uid.ToString() } };
  45. if (album_id > 0 && uid > 0) { arr = new string[,] { { "uid", uid.ToString() }, { "album_id", album_id.ToString() } }; }
  46. else if (album_id > 0) { arr = new string[,] { { "album_id", album_id.ToString() } }; }
  47. Array[] result = api.getApi("audio.get", "audio",
  48. new string[] { "aid", "owner_id", "artist", "title", "duration", "url" },
  49. arr
  50. );
  51. int i = 0;
  52. int num = 1;
  53. foreach (string[] content in result)
  54. {
  55. this.listAudio.TableModel.Rows.Add(new Row());
  56. this.listAudio.TableModel.Rows[i].Tag = content[5];
  57. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(num));
  58. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(content[0]));
  59. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(""));
  60. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(content[2] + " - " + content[3]));
  61. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(this.Duration(int.Parse(content[4]))));
  62. if (api.UserID == api.homeUserID)
  63. {
  64. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell("X"));
  65. this.listAudio.TableModel.Rows[i].Cells[5].ForeColor = Color.Red;
  66. }
  67. else
  68. {
  69. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell("+"));
  70. this.listAudio.TableModel.Rows[i].Cells[5].ForeColor = Color.Green;
  71. }
  72. this.listAudio.TableModel.Rows[i].Cells[5].Tag = i;
  73. //Progress
  74. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(0));
  75. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PLAYING || Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PAUSED)
  76. {
  77. if (int.Parse(content[0]) == currentID)
  78. {
  79. this.listAudio.TableModel.Rows[i].Cells[2].Text = "4";
  80. }
  81. }
  82. this.listAudio.TableModel.Rows[i].Cells[2].Font = new System.Drawing.Font("Webdings", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
  83. num++;
  84. i++;
  85. }
  86. this.listFriends.Cursor = System.Windows.Forms.Cursors.Default;
  87. this.listAudio.TableModel.Dispose();
  88. }
  89. catch (Exception)
  90. {
  91. err.setError("Ошибка при получении данных");
  92. }
  93. }
  94. private void getAlbums(int uid = 0)
  95. {
  96. try
  97. {
  98. this.tabControl.Controls.Clear();
  99. Array[] result = api.getApi("audio.getAlbums", "album",
  100. new string[] { "owner_id", "album_id", "title" },
  101. new string[,] { { "uid", uid.ToString() } });
  102. //Все аудиозаписи
  103. TabPage tabPageAll = new System.Windows.Forms.TabPage();
  104. tabPageAll.Text = "Все аудиозаписи";
  105. tabPageAll.Tag = "0";
  106. this.tabControl.Controls.Add(tabPageAll);
  107. foreach (string[] content in result)
  108. {
  109. TabPage tabPage = new System.Windows.Forms.TabPage();
  110. tabPage.Text = content[2];
  111. tabPage.Tag = content[1];
  112. this.tabControl.Controls.Add(tabPage);
  113. }
  114. }
  115. catch (Exception)
  116. {
  117. err.setError("Не удалось загрузить список альбомов");
  118. }
  119. }
  120. private void tabControl_MouseClick(object sender, MouseEventArgs e)
  121. {
  122. if (e.Button == MouseButtons.Left)
  123. {
  124. this.tabControl.Cursor = System.Windows.Forms.Cursors.WaitCursor;
  125. try
  126. {
  127. this.getAudio(api.UserID, int.Parse(this.tabControl.SelectedTab.Tag.ToString()));
  128. }
  129. catch (Exception)
  130. {
  131. err.setError("Произошла ошибка при открытии альбома");
  132. }
  133. this.tabControl.Cursor = System.Windows.Forms.Cursors.Default;
  134. }
  135. }
  136. private string Duration(int time)
  137. {
  138. string hour = "";
  139. if (time >= 3600)
  140. {
  141. time = time % 3600;
  142. double h = time / 3600;
  143. hour = Math.Floor(h) + ":";
  144. }
  145. double m = time / 60;
  146. time = time % 60;
  147. string min = Math.Floor(m).ToString() + ":";
  148. double s = time;
  149. string sec = Math.Floor(s).ToString();
  150. if (Math.Floor(s) < 10)
  151. {
  152. sec = "0" + Math.Floor(s).ToString();
  153. }
  154. return String.Format("{0}{1}{2}", hour, min, sec);
  155. }
  156. private void listAudio_ClientSizeChanged(object sender, EventArgs e)
  157. {
  158. this.audio_titleColumn.Width = this.listAudio.Width
  159. - audio_numColumn.Width
  160. - audio_playingColumn.Width
  161. - audio_durationColumn.Width
  162. - audio_buttonColumn.Width
  163. - audio_progressColumn.Width
  164. - 50;
  165. }
  166. private void listAudio_CellDoubleClick(object sender, XPTable.Events.CellMouseEventArgs e)
  167. {
  168. this.Play(e.Row);
  169. }
  170. private void timerBass_Tick(object sender, EventArgs e)
  171. {
  172. switch (Bass.BASS_ChannelIsActive(uBass.stream))
  173. {
  174. case BASSActive.BASS_ACTIVE_PAUSED:
  175. {
  176. break;
  177. }
  178. case BASSActive.BASS_ACTIVE_PLAYING:
  179. {
  180. string sb = string.Empty;
  181. sb = Bass.BASS_ChannelGetPosition(uBass.stream).ToString();
  182. trackBarPosition.Maximum = int.Parse(((Bass.BASS_ChannelBytes2Seconds(uBass.stream, Bass.BASS_ChannelGetLength(uBass.stream)).ToString().Split(',', '.'))[0]));
  183. trackBarPosition.Value = int.Parse(((Bass.BASS_ChannelBytes2Seconds(uBass.stream, Bass.BASS_ChannelGetPosition(uBass.stream)).ToString().Split(',', '.'))[0]));
  184. int time = (int)Bass.BASS_ChannelBytes2Seconds(uBass.stream, Bass.BASS_ChannelGetPosition(uBass.stream));
  185. this.audioTime.Text = Duration(time) + " / " + fullTime;
  186. break;
  187. }
  188. case BASSActive.BASS_ACTIVE_STALLED:
  189. {
  190. if (manualStop != true)
  191. {
  192. Bass.BASS_ChannelStop(uBass.stream);
  193. Bass.BASS_StreamFree(uBass.stream);
  194. this.Next();
  195. }
  196. break;
  197. }
  198. case BASSActive.BASS_ACTIVE_STOPPED:
  199. {
  200. if (uBass.stream != 0 && manualStop != true)
  201. {
  202. this.Next();
  203. }
  204. break;
  205. }
  206. }
  207. }
  208. int currentIndex = 0;
  209. int currentID = 0;
  210. string fullTime = "0:00";
  211. private void Play(int index)
  212. {
  213. this.buttonPause.Visible = true;
  214. this.buttonPlay.Visible = false;
  215. try
  216. {
  217. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  218. {
  219. r.Cells[2].Text = "";
  220. }
  221. currentIndex = index;
  222. this.listAudio.TableModel.Rows[index].Cells[2].Text = "4";
  223. currentID = int.Parse(this.listAudio.TableModel.Rows[index].Cells[1].Text);
  224. string url = this.listAudio.TableModel.Rows[index].Tag.ToString();
  225. this.audioText.Text = this.listAudio.TableModel.Rows[index].Cells[3].Text;
  226. this.listAudio.EnsureVisible(index, 3);
  227. fullTime = this.listAudio.TableModel.Rows[currentIndex].Cells[4].Text;
  228. uBass.Play(url);
  229. manualStop = false;
  230. }
  231. catch (Exception)
  232. {
  233. err.setError("Не удалось воспроизвести аудиозапись");
  234. }
  235. }
  236. Random r = new Random();
  237. private void Next()
  238. {
  239. if (this.checkShuffle.Checked == true)
  240. {
  241. int newIndex = r.Next(0, this.listAudio.TableModel.Rows.Count - 1);
  242. currentIndex = (newIndex != currentIndex) ? newIndex : newIndex + 1;
  243. }
  244. else
  245. {
  246. currentIndex++;
  247. }
  248. if (currentIndex > this.listAudio.TableModel.Rows.Count - 1)
  249. {
  250. currentIndex = 0;
  251. }
  252. this.Play(currentIndex);
  253. }
  254. private void Back()
  255. {
  256. if (this.checkShuffle.Checked == true)
  257. {
  258. int newIndex = r.Next(0, this.listAudio.TableModel.Rows.Count - 1);
  259. currentIndex = (newIndex != currentIndex) ? newIndex : newIndex - 1;
  260. }
  261. else
  262. {
  263. currentIndex--;
  264. }
  265. if (currentIndex < 0)
  266. {
  267. currentIndex = 0;
  268. }
  269. this.Play(currentIndex);
  270. }
  271. private void trackBarPosition_Scroll(object sender, EventArgs e)
  272. {
  273. Bass.BASS_ChannelSetPosition(uBass.stream, (double)trackBarPosition.Value);
  274. }
  275. private void trackBarPosition_MouseDown(object sender, MouseEventArgs e)
  276. {
  277. trackBarPosition.Value = (int)trackBarPosition.Maximum * e.X / trackBarPosition.Width;
  278. Bass.BASS_ChannelSetPosition(uBass.stream, (double)trackBarPosition.Value);
  279. }
  280. //float getVolume;
  281. private void trackBarVolume_Scroll(object sender, EventArgs e)
  282. {
  283. //getVolume = (float)Math.Floor(Bass.BASS_GetVolume());
  284. Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_GVOL_STREAM, (int)trackBarVolume.Value);
  285. }
  286. private void buttonBack_Click(object sender, EventArgs e)
  287. {
  288. this.Back();
  289. }
  290. private void buttonPlay_Click(object sender, EventArgs e)
  291. {
  292. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PAUSED)
  293. {
  294. this.buttonPause.Visible = true;
  295. this.buttonPlay.Visible = false;
  296. Bass.BASS_ChannelPlay(uBass.stream, false);
  297. }
  298. else
  299. {
  300. if (this.checkShuffle.Checked == true)
  301. {
  302. int newIndex = r.Next(0, this.listAudio.TableModel.Rows.Count - 1);
  303. currentIndex = (newIndex != currentIndex) ? newIndex : newIndex + 1;
  304. }
  305. this.Play(currentIndex);
  306. }
  307. }
  308. private void buttonPause_Click(object sender, EventArgs e)
  309. {
  310. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PLAYING)
  311. {
  312. this.buttonPlay.Visible = true;
  313. this.buttonPause.Visible = false;
  314. Bass.BASS_ChannelPause(uBass.stream);
  315. }
  316. }
  317. private void buttonStop_Click(object sender, EventArgs e)
  318. {
  319. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PLAYING || Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PAUSED)
  320. {
  321. manualStop = true;
  322. this.buttonPlay.Visible = true;
  323. this.buttonPause.Visible = false;
  324. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  325. {
  326. r.Cells[2].Text = "";
  327. }
  328. currentID = 0;
  329. this.trackBarPosition.Value = 0;
  330. this.audioText.Text = "";
  331. this.audioTime.Text = "";
  332. Bass.BASS_ChannelStop(uBass.stream);
  333. Bass.BASS_StreamFree(uBass.stream);
  334. }
  335. }
  336. private void buttonNext_Click(object sender, EventArgs e)
  337. {
  338. this.Next();
  339. }
  340. private void listAudio_CellButtonClicked(object sender, XPTable.Events.CellButtonEventArgs e)
  341. {
  342. try
  343. {
  344. int index = int.Parse(e.Cell.Tag.ToString());
  345. int aid = int.Parse(this.listAudio.TableModel.Rows[index].Cells[1].Text);
  346. string title = this.listAudio.TableModel.Rows[index].Cells[3].Text;
  347. if (api.UserID == api.homeUserID && this.searchTrue == false)
  348. {
  349. Array[] result = api.getApi("audio.delete", "response", null,
  350. new string[,] { { "aid", aid.ToString() }, { "oid", api.UserID.ToString() } });
  351. foreach (string[] content in result)
  352. {
  353. if (content[0] == "1")
  354. {
  355. this.listAudio.TableModel.Rows[index].Dispose();
  356. int i = 0;
  357. int num = 1;
  358. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  359. {
  360. r.Cells[5].Tag = i;
  361. r.Cells[0].Data = num;
  362. i++;
  363. num++;
  364. }
  365. }
  366. }
  367. }
  368. else if (this.searchTrue == true)
  369. {
  370. Array[] result = api.getApi("audio.add", "response", null,
  371. new string[,] { { "aid", aid.ToString() }, { "oid", this.listAudio.TableModel.Rows[index].Cells[0].Tag.ToString() } });
  372. foreach (string[] content in result)
  373. {
  374. if (content[0] == "1")
  375. {
  376. this.listAudio.TableModel.Rows[index].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(38)))), ((int)(((byte)(53)))), ((int)(((byte)(64)))));
  377. this.listAudio.TableModel.Rows[index].ForeColor = Color.White;
  378. }
  379. }
  380. }
  381. else
  382. {
  383. Array[] result = api.getApi("audio.add", "response", null,
  384. new string[,] { { "aid", aid.ToString() }, { "oid", api.UserID.ToString() } });
  385. foreach (string[] content in result)
  386. {
  387. if (content[0] == "1")
  388. {
  389. this.listAudio.TableModel.Rows[index].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(38)))), ((int)(((byte)(53)))), ((int)(((byte)(64)))));
  390. this.listAudio.TableModel.Rows[index].ForeColor = Color.White;
  391. }
  392. }
  393. }
  394. }
  395. catch (Exception)
  396. {
  397. err.setError("Ошибка при инициализации команды");
  398. }
  399. }
  400. bool searchTrue = false;
  401. private void boxSearch_KeyPress(object sender, KeyPressEventArgs e)
  402. {
  403. try
  404. {
  405. if (e.KeyChar.Equals((char)13) && this.boxSearch.Text != "Поиск")
  406. {
  407. this.listFriends.Cursor = System.Windows.Forms.Cursors.WaitCursor;
  408. this.listAudio.TableModel.Rows.Clear();
  409. Array[] result = api.getApi("audio.search", "audio",
  410. new string[] { "aid", "owner_id", "artist", "title", "duration", "url" },
  411. new string[,] { { "q", this.boxSearch.Text }, { "auto_complete", "1" }, { "count", "200" } }
  412. );
  413. int i = 0;
  414. int num = 1;
  415. foreach (string[] content in result)
  416. {
  417. this.listAudio.TableModel.Rows.Add(new Row());
  418. this.listAudio.TableModel.Rows[i].Tag = content[5];
  419. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(num));
  420. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(content[0]));
  421. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(""));
  422. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(content[2] + " - " + content[3]));
  423. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(this.Duration(int.Parse(content[4]))));
  424. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell("+"));
  425. this.listAudio.TableModel.Rows[i].Cells[5].ForeColor = Color.Green;
  426. this.listAudio.TableModel.Rows[i].Cells[0].Tag = content[1];
  427. this.listAudio.TableModel.Rows[i].Cells[5].Tag = i;
  428. //Progress
  429. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(0));
  430. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PLAYING || Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PAUSED)
  431. {
  432. if (int.Parse(content[0]) == currentID)
  433. {
  434. this.listAudio.TableModel.Rows[i].Cells[2].Text = "4";
  435. }
  436. }
  437. this.listAudio.TableModel.Rows[i].Cells[2].Font = new System.Drawing.Font("Webdings", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
  438. num++;
  439. i++;
  440. }
  441. this.listFriends.Cursor = System.Windows.Forms.Cursors.Default;
  442. this.searchTrue = true;
  443. this.listAudio.TableModel.Dispose();
  444. }
  445. }
  446. catch (Exception)
  447. {
  448. err.setError("Ошибка при получении данных");
  449. }
  450. }
  451. private void checkBoxSelectAll_CheckedChanged(object sender, EventArgs e)
  452. {
  453. if (this.checkBoxSelectAll.Checked == true)
  454. {
  455. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  456. {
  457. r.Cells[3].Checked = true;
  458. }
  459. }
  460. else
  461. {
  462. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  463. {
  464. r.Cells[3].Checked = false;
  465. }
  466. }
  467. }
  468. string[] download;
  469. private void buttonDownload_Click(object sender, EventArgs e)
  470. {
  471. int iChecked = 0;
  472. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  473. {
  474. if (r.Cells[3].Checked == true)
  475. {
  476. iChecked++;
  477. }
  478. }
  479. if (iChecked > 0)
  480. {
  481. this.saveDownload.ShowDialog();
  482. if (this.saveDownload.SelectedPath != "")
  483. {
  484. this.audio_progressColumn.Visible = true;
  485. Thread threadDownload = new Thread(downloadThread);
  486. threadDownload.Priority = ThreadPriority.Lowest;
  487. threadDownload.Start();
  488. }
  489. else
  490. {
  491. err.setError("Вы не отметили ни одной аудиозаписи для скачивания");
  492. }
  493. }
  494. }
  495. private void downloadThread() {
  496. this.listAudio.Invoke(new delegateThreadDownload(this.downloadFunc));
  497. }
  498. public delegate void delegateThreadDownload();
  499. private void downloadFunc()
  500. {
  501. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  502. {
  503. if (File.Exists(this.saveDownload.SelectedPath + "\\" + r.Cells[1].Text + ".mp3"))
  504. {
  505. r.Cells[6].Data = 100;
  506. }
  507. if (r.Cells[3].Checked == true)
  508. {
  509. try
  510. {
  511. if (!File.Exists(this.saveDownload.SelectedPath + "\\" + r.Cells[1].Text + ".mp3"))
  512. {
  513. using (WebClient download = new WebClient())
  514. {
  515. download.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  516. download.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  517. download.DownloadFileAsync(new Uri(r.Tag.ToString()), this.saveDownload.SelectedPath + "\\" + r.Cells[1].Text + ".mp3.tmp", r.Index);
  518. }
  519. }
  520. }
  521. catch (WebException e)
  522. {
  523. err.setError("Произошла ошибка при загрузке аудиозаписи");
  524. }
  525. }
  526. }
  527. }
  528. private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  529. {
  530. this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[6].Data = e.ProgressPercentage;
  531. }
  532. private void Completed(object sender, AsyncCompletedEventArgs e)
  533. {
  534. string file = this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[1].Text+".mp3";
  535. FileInfo fi = new FileInfo(this.saveDownload.SelectedPath+"\\"+file+".tmp");
  536. if (fi.Exists)
  537. {
  538. fi.MoveTo(this.saveDownload.SelectedPath + "\\" + file);
  539. this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[6].Data = 100;
  540. }
  541. else
  542. {
  543. this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[6].Data = 0;
  544. }
  545. }
  546. }
  547. }