PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/winbird v0.0.1.4/WinBird/Audio.cs

#
C# | 668 lines | 552 code | 111 blank | 5 comment | 75 complexity | 358bd6722a7b9cc24c6aa0ce5ad0c2ba 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.listAudio.TableModel.Rows.Count > 0)
  240. {
  241. if (this.checkShuffle.Checked == true)
  242. {
  243. int newIndex = r.Next(0, this.listAudio.TableModel.Rows.Count - 1);
  244. currentIndex = (newIndex != currentIndex) ? newIndex : newIndex + 1;
  245. }
  246. else
  247. {
  248. currentIndex++;
  249. }
  250. if (currentIndex > this.listAudio.TableModel.Rows.Count - 1)
  251. {
  252. currentIndex = 0;
  253. }
  254. this.Play(currentIndex);
  255. }
  256. }
  257. private void Back()
  258. {
  259. if (this.listAudio.TableModel.Rows.Count > 0)
  260. {
  261. if (this.checkShuffle.Checked == true)
  262. {
  263. int newIndex = r.Next(0, this.listAudio.TableModel.Rows.Count - 1);
  264. currentIndex = (newIndex != currentIndex) ? newIndex : newIndex - 1;
  265. }
  266. else
  267. {
  268. currentIndex--;
  269. }
  270. if (currentIndex < 0)
  271. {
  272. currentIndex = 0;
  273. }
  274. this.Play(currentIndex);
  275. }
  276. }
  277. private void trackBarPosition_Scroll(object sender, EventArgs e)
  278. {
  279. Bass.BASS_ChannelSetPosition(uBass.stream, (double)trackBarPosition.Value);
  280. }
  281. private void trackBarPosition_MouseDown(object sender, MouseEventArgs e)
  282. {
  283. trackBarPosition.Value = (int)trackBarPosition.Maximum * e.X / trackBarPosition.Width;
  284. Bass.BASS_ChannelSetPosition(uBass.stream, (double)trackBarPosition.Value);
  285. }
  286. //float getVolume;
  287. private void trackBarVolume_Scroll(object sender, EventArgs e)
  288. {
  289. //getVolume = (float)Math.Floor(Bass.BASS_GetVolume());
  290. Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_GVOL_STREAM, (int)trackBarVolume.Value);
  291. }
  292. private void buttonBack_Click(object sender, EventArgs e)
  293. {
  294. this.Back();
  295. }
  296. private void buttonPlay_Click(object sender, EventArgs e)
  297. {
  298. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PAUSED)
  299. {
  300. this.buttonPause.Visible = true;
  301. this.buttonPlay.Visible = false;
  302. Bass.BASS_ChannelPlay(uBass.stream, false);
  303. }
  304. else
  305. {
  306. if (this.checkShuffle.Checked == true)
  307. {
  308. int newIndex = r.Next(0, this.listAudio.TableModel.Rows.Count - 1);
  309. currentIndex = (newIndex != currentIndex) ? newIndex : newIndex + 1;
  310. }
  311. this.Play(currentIndex);
  312. }
  313. }
  314. private void buttonPause_Click(object sender, EventArgs e)
  315. {
  316. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PLAYING)
  317. {
  318. this.buttonPlay.Visible = true;
  319. this.buttonPause.Visible = false;
  320. Bass.BASS_ChannelPause(uBass.stream);
  321. }
  322. }
  323. private void buttonStop_Click(object sender, EventArgs e)
  324. {
  325. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PLAYING || Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PAUSED)
  326. {
  327. manualStop = true;
  328. this.buttonPlay.Visible = true;
  329. this.buttonPause.Visible = false;
  330. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  331. {
  332. r.Cells[2].Text = "";
  333. }
  334. currentID = 0;
  335. this.trackBarPosition.Value = 0;
  336. this.audioText.Text = "";
  337. this.audioTime.Text = "";
  338. Bass.BASS_ChannelStop(uBass.stream);
  339. Bass.BASS_StreamFree(uBass.stream);
  340. }
  341. }
  342. private void buttonNext_Click(object sender, EventArgs e)
  343. {
  344. this.Next();
  345. }
  346. private void listAudio_CellButtonClicked(object sender, XPTable.Events.CellButtonEventArgs e)
  347. {
  348. try
  349. {
  350. int index = int.Parse(e.Cell.Tag.ToString());
  351. int aid = int.Parse(this.listAudio.TableModel.Rows[index].Cells[1].Text);
  352. string title = this.listAudio.TableModel.Rows[index].Cells[3].Text;
  353. if (api.UserID == api.homeUserID && this.searchTrue == false)
  354. {
  355. Array[] result = api.getApi("audio.delete", "response", null,
  356. new string[,] { { "aid", aid.ToString() }, { "oid", api.UserID.ToString() } });
  357. foreach (string[] content in result)
  358. {
  359. if (content[0] == "1")
  360. {
  361. this.listAudio.TableModel.Rows[index].Dispose();
  362. int i = 0;
  363. int num = 1;
  364. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  365. {
  366. r.Cells[5].Tag = i;
  367. r.Cells[0].Data = num;
  368. i++;
  369. num++;
  370. }
  371. }
  372. }
  373. }
  374. else if (this.searchTrue == true)
  375. {
  376. Array[] result = api.getApi("audio.add", "response", null,
  377. new string[,] { { "aid", aid.ToString() }, { "oid", this.listAudio.TableModel.Rows[index].Cells[0].Tag.ToString() } });
  378. foreach (string[] content in result)
  379. {
  380. if (content[0] == "1")
  381. {
  382. this.listAudio.TableModel.Rows[index].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(38)))), ((int)(((byte)(53)))), ((int)(((byte)(64)))));
  383. this.listAudio.TableModel.Rows[index].ForeColor = Color.White;
  384. }
  385. }
  386. }
  387. else
  388. {
  389. Array[] result = api.getApi("audio.add", "response", null,
  390. new string[,] { { "aid", aid.ToString() }, { "oid", api.UserID.ToString() } });
  391. foreach (string[] content in result)
  392. {
  393. if (content[0] == "1")
  394. {
  395. this.listAudio.TableModel.Rows[index].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(38)))), ((int)(((byte)(53)))), ((int)(((byte)(64)))));
  396. this.listAudio.TableModel.Rows[index].ForeColor = Color.White;
  397. }
  398. }
  399. }
  400. }
  401. catch (Exception)
  402. {
  403. err.setError("Ошибка при инициализации команды");
  404. }
  405. }
  406. bool searchTrue = false;
  407. private void boxSearch_KeyPress(object sender, KeyPressEventArgs e)
  408. {
  409. try
  410. {
  411. if (e.KeyChar.Equals((char)13) && this.boxSearch.Text != "Поиск")
  412. {
  413. this.listFriends.Cursor = System.Windows.Forms.Cursors.WaitCursor;
  414. this.listAudio.TableModel.Rows.Clear();
  415. Array[] result = api.getApi("audio.search", "audio",
  416. new string[] { "aid", "owner_id", "artist", "title", "duration", "url" },
  417. new string[,] { { "q", this.boxSearch.Text }, { "auto_complete", "1" }, { "count", "200" } }
  418. );
  419. int i = 0;
  420. int num = 1;
  421. foreach (string[] content in result)
  422. {
  423. this.listAudio.TableModel.Rows.Add(new Row());
  424. this.listAudio.TableModel.Rows[i].Tag = content[5];
  425. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(num));
  426. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(content[0]));
  427. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(""));
  428. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(content[2] + " - " + content[3]));
  429. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(this.Duration(int.Parse(content[4]))));
  430. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell("+"));
  431. this.listAudio.TableModel.Rows[i].Cells[5].ForeColor = Color.Green;
  432. this.listAudio.TableModel.Rows[i].Cells[0].Tag = content[1];
  433. this.listAudio.TableModel.Rows[i].Cells[5].Tag = i;
  434. //Progress
  435. this.listAudio.TableModel.Rows[i].Cells.Add(new Cell(0));
  436. if (Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PLAYING || Bass.BASS_ChannelIsActive(uBass.stream) == BASSActive.BASS_ACTIVE_PAUSED)
  437. {
  438. if (int.Parse(content[0]) == currentID)
  439. {
  440. this.listAudio.TableModel.Rows[i].Cells[2].Text = "4";
  441. }
  442. }
  443. 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)));
  444. num++;
  445. i++;
  446. }
  447. this.listFriends.Cursor = System.Windows.Forms.Cursors.Default;
  448. this.searchTrue = true;
  449. this.listAudio.TableModel.Dispose();
  450. }
  451. }
  452. catch (Exception)
  453. {
  454. err.setError("Ошибка при получении данных");
  455. }
  456. }
  457. private void checkBoxSelectAll_CheckedChanged(object sender, EventArgs e)
  458. {
  459. if (this.checkBoxSelectAll.Checked == true)
  460. {
  461. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  462. {
  463. r.Cells[3].Checked = true;
  464. }
  465. }
  466. else
  467. {
  468. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  469. {
  470. r.Cells[3].Checked = false;
  471. }
  472. }
  473. }
  474. string[] download;
  475. private void buttonDownload_Click(object sender, EventArgs e)
  476. {
  477. int iChecked = 0;
  478. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  479. {
  480. if (r.Cells[3].Checked == true)
  481. {
  482. iChecked++;
  483. }
  484. }
  485. if (iChecked > 0)
  486. {
  487. this.saveDownload.ShowDialog();
  488. if (this.saveDownload.SelectedPath != "")
  489. {
  490. this.audio_progressColumn.Visible = true;
  491. Thread threadDownload = new Thread(downloadThread);
  492. threadDownload.Priority = ThreadPriority.Lowest;
  493. threadDownload.Start();
  494. }
  495. else
  496. {
  497. err.setError("Вы не отметили ни одной аудиозаписи для скачивания");
  498. }
  499. }
  500. }
  501. private void downloadThread() {
  502. this.listAudio.Invoke(new delegateThreadDownload(this.downloadFunc));
  503. }
  504. public delegate void delegateThreadDownload();
  505. private void downloadFunc()
  506. {
  507. foreach (XPTable.Models.Row r in this.listAudio.TableModel.Rows)
  508. {
  509. if (File.Exists(this.saveDownload.SelectedPath + "\\" + r.Cells[1].Text + ".mp3"))
  510. {
  511. r.Cells[6].Data = 100;
  512. }
  513. if (r.Cells[3].Checked == true)
  514. {
  515. try
  516. {
  517. if (!File.Exists(this.saveDownload.SelectedPath + "\\" + r.Cells[1].Text + ".mp3"))
  518. {
  519. using (WebClient download = new WebClient())
  520. {
  521. download.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  522. download.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  523. download.DownloadFileAsync(new Uri(r.Tag.ToString()), this.saveDownload.SelectedPath + "\\" + r.Cells[1].Text + ".mp3.tmp", r.Index);
  524. }
  525. }
  526. }
  527. catch (WebException e)
  528. {
  529. err.setError("Произошла ошибка при загрузке аудиозаписи");
  530. }
  531. }
  532. }
  533. }
  534. private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  535. {
  536. this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[6].Data = e.ProgressPercentage;
  537. }
  538. private void Completed(object sender, AsyncCompletedEventArgs e)
  539. {
  540. string file = this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[1].Text+".mp3";
  541. FileInfo fi = new FileInfo(this.saveDownload.SelectedPath+"\\"+file+".tmp");
  542. if (fi.Exists)
  543. {
  544. fi.MoveTo(this.saveDownload.SelectedPath + "\\" + file);
  545. this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[6].Data = 100;
  546. }
  547. else
  548. {
  549. this.listAudio.TableModel.Rows[int.Parse(e.UserState.ToString())].Cells[6].Data = 0;
  550. }
  551. }
  552. }
  553. }