Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.53 KB | None | 0 0
  1. using AsdfRep.Models;
  2. using MahApps.Metro.Controls.Dialogs;
  3. using Microsoft.Win32;
  4. using MySql.Data.MySqlClient;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using System.Windows.Threading;
  21.  
  22. namespace AsdfRep
  23. {
  24. public partial class MainWindow
  25. {
  26. MySqlConnection conn;
  27. MySqlCommand cmd;
  28. MySqlDataReader reader;
  29. string sql;
  30.  
  31. MediaPlayer player = new MediaPlayer();
  32.  
  33. int currentPlaylist = 1;
  34.  
  35. public MainWindow()
  36. {
  37. InitializeComponent();
  38. conn = new MySqlConnection(
  39. "Server=localhost; Database=reproductor; User=root; Password=12345678");
  40.  
  41. player.MediaEnded += new EventHandler(player_MediaEnded);
  42. DispatcherTimer dispatcherTimer = new DispatcherTimer();
  43. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  44. dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
  45. dispatcherTimer.Start();
  46. }
  47.  
  48. private void dispatcherTimer_Tick(object sender, EventArgs e)
  49. {
  50. timeSlider.Value = player.Position.TotalSeconds;
  51. passedTimeLabel.Content = player.Position.ToString(@"m\:ss");
  52. }
  53.  
  54. private void player_MediaEnded(object sender, EventArgs e)
  55. {
  56. skipNextButton_Click(sender, new RoutedEventArgs());
  57. }
  58.  
  59. private void Window_Loaded(object sender, RoutedEventArgs e)
  60. {
  61. try
  62. {
  63. sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
  64. "FROM canciones, artistas, albums " +
  65. "WHERE canciones.id_artista = artistas.id AND " +
  66. "canciones.id_album = albums.id;" +
  67. "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
  68. "FROM canciones, artistas, albums " +
  69. "WHERE canciones.id_artista = artistas.id AND " +
  70. "canciones.id_album = albums.id;" +
  71. "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
  72. "FROM canciones, artistas, albums, playlists, cancion_playlist " +
  73. "WHERE playlists.id = cancion_playlist.id_playlist AND " +
  74. "cancion_playlist.id_cancion = canciones.id AND " +
  75. "canciones.id_album = albums.id AND " +
  76. "canciones.id_artista = artistas.id AND " +
  77. "playlists.id = '" + currentPlaylist + "';" +
  78. "SELECT nombre FROM playlists";
  79. cmd = new MySqlCommand(sql, conn);
  80. conn.Open();
  81. reader = cmd.ExecuteReader();
  82.  
  83. if (reader.HasRows)
  84. {
  85. List<Cancion> songItems = new List<Cancion>();
  86. while (reader.Read())
  87. {
  88. songItems.Add(new Cancion()
  89. {
  90. Titulo = reader.GetString(0),
  91. Genero = reader.GetString(1),
  92. Artista = reader.GetString(2),
  93. Album = reader.GetString(3),
  94. });
  95. }
  96. if (songItems != null)
  97. {
  98. songsListView.ItemsSource = songItems;
  99.  
  100. CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(songsListView.ItemsSource);
  101. view.Filter = SongFilter;
  102. }
  103.  
  104. if (reader.NextResult())
  105. {
  106. List<Cancion> libraryItems = new List<Cancion>();
  107. while (reader.Read())
  108. {
  109. libraryItems.Add(new Cancion()
  110. {
  111. Titulo = reader.GetString(0),
  112. Genero = reader.GetString(1),
  113. Artista = reader.GetString(2),
  114. Album = reader.GetString(3),
  115. });
  116. }
  117. if (libraryItems != null)
  118. {
  119. libraryGrid.ItemsSource = libraryItems;
  120.  
  121. CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(libraryGrid.ItemsSource);
  122. view.Filter = LibraryFilter;
  123. }
  124. }
  125.  
  126. if (reader.NextResult())
  127. {
  128. List<Cancion> playlistItems = new List<Cancion>();
  129. while (reader.Read())
  130. {
  131. playlistItems.Add(new Cancion()
  132. {
  133. Titulo = reader.GetString(0),
  134. Genero = reader.GetString(1),
  135. Artista = reader.GetString(2),
  136. Album = reader.GetString(3),
  137. });
  138. }
  139. if (playlistItems != null)
  140. playlistListView.ItemsSource = playlistItems;
  141. }
  142.  
  143. if (reader.NextResult())
  144. {
  145. List<Playlist> playlistItems = new List<Playlist>();
  146. while (reader.Read())
  147. {
  148. playlistItems.Add(new Playlist()
  149. {
  150. Nombre = reader.GetString(0),
  151. });
  152. }
  153. if (playlistItems != null)
  154. showPlaylistComboBox.ItemsSource = playlistItems;
  155. }
  156. }
  157. }
  158. catch (MySqlException ex)
  159. {
  160. MessageBox.Show(ex.Message);
  161. }
  162. finally
  163. {
  164. conn.Close();
  165. }
  166. }
  167.  
  168. private bool SongFilter(object item)
  169. {
  170. if (String.IsNullOrEmpty(searchTextBox.Text))
  171. return true;
  172. else
  173. return ((item as Cancion).Titulo.IndexOf(searchTextBox.Text, StringComparison.OrdinalIgnoreCase) >= 0);
  174. }
  175.  
  176. private bool LibraryFilter(object item)
  177. {
  178. if (String.IsNullOrEmpty(librarySearchTextBox.Text))
  179. return true;
  180. else
  181. return ((item as Cancion).Titulo.IndexOf(librarySearchTextBox.Text, StringComparison.OrdinalIgnoreCase) >= 0);
  182. }
  183.  
  184.  
  185. private void playButton_Click(object sender, RoutedEventArgs e)
  186. {
  187. string titulo, ubicacion = "";
  188.  
  189. if (songsListView.IsVisible)
  190. {
  191. if (songsListView.SelectedItem != null)
  192. {
  193. titulo = (songsListView.SelectedItem as Cancion).Titulo;
  194. }
  195. else
  196. {
  197. songsListView.SelectedIndex = 0;
  198. titulo = (songsListView.Items[0] as Cancion).Titulo;
  199. }
  200. }
  201. else
  202. {
  203. if (playlistListView.SelectedItem != null)
  204. {
  205. titulo = (playlistListView.SelectedItem as Cancion).Titulo;
  206. }
  207. else
  208. {
  209. playlistListView.SelectedIndex = 0;
  210. titulo = (playlistListView.Items[0] as Cancion).Titulo;
  211. }
  212. }
  213.  
  214. try
  215. {
  216. sql = "SELECT ubicacion FROM canciones WHERE titulo = '" + titulo + "'";
  217. cmd = new MySqlCommand(sql, conn);
  218. conn.Open();
  219. reader = cmd.ExecuteReader();
  220.  
  221. if (reader.HasRows)
  222. {
  223. while (reader.Read())
  224. {
  225. ubicacion = reader.GetString(0);
  226. }
  227. }
  228. }
  229. catch (MySqlException ex)
  230. {
  231. MessageBox.Show(ex.Message);
  232. }
  233. finally
  234. {
  235. conn.Close();
  236. }
  237.  
  238. if (ubicacion != "")
  239. {
  240. try
  241. {
  242. player.Open(new Uri(ubicacion));
  243. player.Play();
  244. while (!player.NaturalDuration.HasTimeSpan) ;
  245. timeSlider.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;
  246. totalTimeLabel.Content = player.NaturalDuration.TimeSpan.ToString(@"m\:ss");
  247. }
  248. catch (Exception ex)
  249. {
  250. MessageBox.Show(ex.Message);
  251. }
  252. }
  253. }
  254.  
  255. private void stopButton_Click(object sender, RoutedEventArgs e)
  256. {
  257. player.Stop();
  258. }
  259.  
  260. private void skipPreviousButton_Click(object sender, RoutedEventArgs e)
  261. {
  262. if (songsListView.IsVisible) songsListView.SelectedIndex -= 1;
  263. else playlistListView.SelectedIndex -= 1;
  264. playButton_Click(sender, e);
  265. }
  266.  
  267. private void skipNextButton_Click(object sender, RoutedEventArgs e)
  268. {
  269. if (songsListView.IsVisible) songsListView.SelectedIndex += 1;
  270. else playlistListView.SelectedIndex += 1;
  271. playButton_Click(sender, e);
  272. }
  273.  
  274. private void timeSlider_DragCompleted(object sender, RoutedEventArgs e)
  275. {
  276. TimeSpan t = TimeSpan.FromSeconds(timeSlider.Value);
  277. player.Position = t;
  278. }
  279.  
  280. private void volumeSlider_DragCompleted(object sender, RoutedEventArgs e)
  281. {
  282. player.Volume = volumeSlider.Value;
  283. }
  284.  
  285. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  286. {
  287. if (songsListView.IsVisible)
  288. {
  289. songsListView.Visibility = Visibility.Hidden;
  290. playlistListView.Visibility = Visibility.Visible;
  291. }
  292. else
  293. {
  294. songsListView.Visibility = Visibility.Visible;
  295. playlistListView.Visibility = Visibility.Hidden;
  296. }
  297. }
  298.  
  299. private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
  300. {
  301. CollectionViewSource.GetDefaultView(songsListView.ItemsSource).Refresh();
  302. }
  303.  
  304. private void librarySearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
  305. {
  306. CollectionViewSource.GetDefaultView(libraryGrid.ItemsSource).Refresh();
  307. }
  308.  
  309. private void addToPlaylistButton_Click(object sender, RoutedEventArgs e)
  310. {
  311. int cancion = 0;
  312. string titulo = "";
  313. if (libraryGrid.SelectedItem != null)
  314. {
  315. titulo = (libraryGrid.SelectedItem as Cancion).Titulo;
  316. }
  317.  
  318. try
  319. {
  320. conn.Open();
  321. sql = "SELECT id FROM canciones WHERE titulo = '" + titulo + "'";
  322. cmd = new MySqlCommand(sql, conn);
  323. cancion = (int)cmd.ExecuteScalar();
  324.  
  325. sql = "INSERT INTO cancion_playlist (id_cancion, id_playlist) VALUES ('" + cancion + "', '" + currentPlaylist + "')";
  326. cmd = new MySqlCommand(sql, conn);
  327. cmd.ExecuteNonQuery();
  328.  
  329. sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
  330. "FROM canciones, artistas, albums, playlists, cancion_playlist " +
  331. "WHERE playlists.id = cancion_playlist.id_playlist AND " +
  332. "cancion_playlist.id_cancion = canciones.id AND " +
  333. "canciones.id_album = albums.id AND " +
  334. "canciones.id_artista = artistas.id AND " +
  335. "playlists.id = '" + currentPlaylist + "'";
  336. cmd = new MySqlCommand(sql, conn);
  337. reader = cmd.ExecuteReader();
  338.  
  339. while (reader.Read())
  340. {
  341. if (reader.NextResult())
  342. {
  343. List<Cancion> playlistItems = new List<Cancion>();
  344. while (reader.Read())
  345. {
  346. playlistItems.Add(new Cancion()
  347. {
  348. Titulo = reader.GetString(0),
  349. Genero = reader.GetString(1),
  350. Artista = reader.GetString(2),
  351. Album = reader.GetString(3),
  352. });
  353. }
  354. if (playlistItems != null)
  355. playlistListView.ItemsSource = playlistItems;
  356. }
  357. }
  358. }
  359. catch (MySqlException ex)
  360. {
  361. MessageBox.Show(ex.Message);
  362. }
  363. finally
  364. {
  365. conn.Close();
  366. }
  367. }
  368.  
  369. private void removeFromPlaylistButton_Click(object sender, RoutedEventArgs e)
  370. {
  371. int cancion = 0;
  372. string titulo = "";
  373. if (libraryGrid.SelectedItem != null)
  374. {
  375. titulo = (libraryGrid.SelectedItem as Cancion).Titulo;
  376. }
  377.  
  378. try
  379. {
  380. conn.Open();
  381. sql = "SELECT id FROM canciones WHERE titulo = '" + titulo + "'";
  382. cmd = new MySqlCommand(sql, conn);
  383. cancion = (int)cmd.ExecuteScalar();
  384.  
  385. sql = "DELETE FROM cancion_playlist WHERE id_cancion = '" + cancion + "' AND id_playlist = '" + currentPlaylist + "'";
  386. cmd = new MySqlCommand(sql, conn);
  387. cmd.ExecuteNonQuery();
  388.  
  389. sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
  390. "FROM canciones, artistas, albums, playlists, cancion_playlist " +
  391. "WHERE playlists.id = cancion_playlist.id_playlist AND " +
  392. "cancion_playlist.id_cancion = canciones.id AND " +
  393. "canciones.id_album = albums.id AND " +
  394. "canciones.id_artista = artistas.id AND " +
  395. "playlists.id = '" + currentPlaylist + "'";
  396. cmd = new MySqlCommand(sql, conn);
  397. reader = cmd.ExecuteReader();
  398.  
  399. while (reader.Read())
  400. {
  401. if (reader.NextResult())
  402. {
  403. List<Cancion> playlistItems = new List<Cancion>();
  404. while (reader.Read())
  405. {
  406. playlistItems.Add(new Cancion()
  407. {
  408. Titulo = reader.GetString(0),
  409. Genero = reader.GetString(1),
  410. Artista = reader.GetString(2),
  411. Album = reader.GetString(3),
  412. });
  413. }
  414. if (playlistItems != null)
  415. playlistListView.ItemsSource = playlistItems;
  416. }
  417. }
  418. }
  419. catch (MySqlException ex)
  420. {
  421. MessageBox.Show(ex.Message);
  422. }
  423. finally
  424. {
  425. conn.Close();
  426. }
  427. }
  428.  
  429. private async void newPlaylistButton_Click(object sender, RoutedEventArgs e)
  430. {
  431. var result = await this.ShowInputAsync("Crear Playlist", "Nombre:");
  432.  
  433. if (result == null)
  434. return;
  435.  
  436. try
  437. {
  438. sql = "INSERT INTO playlists (nombre) VALUES ('" + result + "')";
  439. cmd = new MySqlCommand(sql, conn);
  440. conn.Open();
  441. cmd.ExecuteNonQuery();
  442. }
  443. catch (MySqlException ex)
  444. {
  445. MessageBox.Show(ex.Message);
  446. }
  447. finally
  448. {
  449. conn.Close();
  450. }
  451.  
  452. if (songsListView.Visibility == Visibility.Visible)
  453. {
  454. songsListView.Visibility = Visibility.Hidden;
  455. playlistListView.Visibility = Visibility.Visible;
  456. }
  457. }
  458.  
  459. private void showPlaylistComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  460. {
  461. string nombre = "";
  462. if (showPlaylistComboBox.SelectedItem != null)
  463. {
  464. nombre = (showPlaylistComboBox.SelectedItem as Playlist).Nombre;
  465. }
  466.  
  467. try
  468. {
  469. conn.Open();
  470. sql = "SELECT id FROM playlists WHERE nombre = '" + nombre + "'";
  471. cmd = new MySqlCommand(sql, conn);
  472. currentPlaylist = (int)cmd.ExecuteScalar();
  473.  
  474. sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
  475. "FROM canciones, artistas, albums, playlists, cancion_playlist " +
  476. "WHERE playlists.id = cancion_playlist.id_playlist AND " +
  477. "cancion_playlist.id_cancion = canciones.id AND " +
  478. "canciones.id_album = albums.id AND " +
  479. "canciones.id_artista = artistas.id AND " +
  480. "playlists.id = '" + currentPlaylist + "'";
  481. cmd = new MySqlCommand(sql, conn);
  482. reader = cmd.ExecuteReader();
  483.  
  484. if (reader.HasRows)
  485. {
  486. List<Cancion> playlistItems = new List<Cancion>();
  487. while (reader.Read())
  488. {
  489. playlistItems.Add(new Cancion()
  490. {
  491. Titulo = reader.GetString(0),
  492. Genero = reader.GetString(1),
  493. Artista = reader.GetString(2),
  494. Album = reader.GetString(3),
  495. });
  496. }
  497. if (playlistItems != null)
  498. playlistListView.ItemsSource = playlistItems;
  499.  
  500. }
  501. else
  502. {
  503. playlistListView.ItemsSource = null;
  504. }
  505. }
  506. catch (MySqlException ex)
  507. {
  508. MessageBox.Show(ex.Message);
  509. }
  510. finally
  511. {
  512. conn.Close();
  513. }
  514. }
  515.  
  516. private void Expander_Expanded(object sender, RoutedEventArgs e)
  517. {
  518. Application.Current.MainWindow.MaxWidth = 2000;
  519. Application.Current.MainWindow.Width = 900;
  520. }
  521.  
  522. private void Expander_Collapsed(object sender, RoutedEventArgs e)
  523. {
  524. Application.Current.MainWindow.MaxWidth = 366;
  525. }
  526.  
  527. //private void newSongButton_Click(object sender, RoutedEventArgs e)
  528. //{
  529. // OpenFileDialog fd = new OpenFileDialog();
  530. // fd.Filter = "Archivos de Audio (*.mp3)";
  531. // if (fd.ShowDialog() == true)
  532. // {
  533. // TagLib.File f = TagLib.File.Create(fd.FileName);
  534.  
  535. // try
  536. // {
  537. // sql = "INSERT INTO album (nombre) VALUES ('" + result + "')";
  538. // cmd = new MySqlCommand(sql, conn);
  539. // conn.Open();
  540. // cmd.ExecuteNonQuery();
  541. // }
  542. // catch (MySqlException ex)
  543. // {
  544. // MessageBox.Show(ex.Message);
  545. // }
  546. // finally
  547. // {
  548. // conn.Close();
  549. // }
  550.  
  551. // }
  552. //}
  553. }
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement