Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AsdfRep.Models;
- using MahApps.Metro.Controls.Dialogs;
- using Microsoft.Win32;
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- 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.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace AsdfRep
- {
- public partial class MainWindow
- {
- MySqlConnection conn;
- MySqlCommand cmd;
- MySqlDataReader reader;
- string sql;
- MediaPlayer player = new MediaPlayer();
- int currentPlaylist = 1;
- public MainWindow()
- {
- InitializeComponent();
- conn = new MySqlConnection(
- "Server=localhost; Database=reproductor; User=root; Password=12345678");
- player.MediaEnded += new EventHandler(player_MediaEnded);
- DispatcherTimer dispatcherTimer = new DispatcherTimer();
- dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
- dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
- dispatcherTimer.Start();
- }
- private void dispatcherTimer_Tick(object sender, EventArgs e)
- {
- timeSlider.Value = player.Position.TotalSeconds;
- passedTimeLabel.Content = player.Position.ToString(@"m\:ss");
- }
- private void player_MediaEnded(object sender, EventArgs e)
- {
- skipNextButton_Click(sender, new RoutedEventArgs());
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- try
- {
- sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
- "FROM canciones, artistas, albums " +
- "WHERE canciones.id_artista = artistas.id AND " +
- "canciones.id_album = albums.id;" +
- "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
- "FROM canciones, artistas, albums " +
- "WHERE canciones.id_artista = artistas.id AND " +
- "canciones.id_album = albums.id;" +
- "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
- "FROM canciones, artistas, albums, playlists, cancion_playlist " +
- "WHERE playlists.id = cancion_playlist.id_playlist AND " +
- "cancion_playlist.id_cancion = canciones.id AND " +
- "canciones.id_album = albums.id AND " +
- "canciones.id_artista = artistas.id AND " +
- "playlists.id = '" + currentPlaylist + "';" +
- "SELECT nombre FROM playlists";
- cmd = new MySqlCommand(sql, conn);
- conn.Open();
- reader = cmd.ExecuteReader();
- if (reader.HasRows)
- {
- List<Cancion> songItems = new List<Cancion>();
- while (reader.Read())
- {
- songItems.Add(new Cancion()
- {
- Titulo = reader.GetString(0),
- Genero = reader.GetString(1),
- Artista = reader.GetString(2),
- Album = reader.GetString(3),
- });
- }
- if (songItems != null)
- {
- songsListView.ItemsSource = songItems;
- CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(songsListView.ItemsSource);
- view.Filter = SongFilter;
- }
- if (reader.NextResult())
- {
- List<Cancion> libraryItems = new List<Cancion>();
- while (reader.Read())
- {
- libraryItems.Add(new Cancion()
- {
- Titulo = reader.GetString(0),
- Genero = reader.GetString(1),
- Artista = reader.GetString(2),
- Album = reader.GetString(3),
- });
- }
- if (libraryItems != null)
- {
- libraryGrid.ItemsSource = libraryItems;
- CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(libraryGrid.ItemsSource);
- view.Filter = LibraryFilter;
- }
- }
- if (reader.NextResult())
- {
- List<Cancion> playlistItems = new List<Cancion>();
- while (reader.Read())
- {
- playlistItems.Add(new Cancion()
- {
- Titulo = reader.GetString(0),
- Genero = reader.GetString(1),
- Artista = reader.GetString(2),
- Album = reader.GetString(3),
- });
- }
- if (playlistItems != null)
- playlistListView.ItemsSource = playlistItems;
- }
- if (reader.NextResult())
- {
- List<Playlist> playlistItems = new List<Playlist>();
- while (reader.Read())
- {
- playlistItems.Add(new Playlist()
- {
- Nombre = reader.GetString(0),
- });
- }
- if (playlistItems != null)
- showPlaylistComboBox.ItemsSource = playlistItems;
- }
- }
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- private bool SongFilter(object item)
- {
- if (String.IsNullOrEmpty(searchTextBox.Text))
- return true;
- else
- return ((item as Cancion).Titulo.IndexOf(searchTextBox.Text, StringComparison.OrdinalIgnoreCase) >= 0);
- }
- private bool LibraryFilter(object item)
- {
- if (String.IsNullOrEmpty(librarySearchTextBox.Text))
- return true;
- else
- return ((item as Cancion).Titulo.IndexOf(librarySearchTextBox.Text, StringComparison.OrdinalIgnoreCase) >= 0);
- }
- private void playButton_Click(object sender, RoutedEventArgs e)
- {
- string titulo, ubicacion = "";
- if (songsListView.IsVisible)
- {
- if (songsListView.SelectedItem != null)
- {
- titulo = (songsListView.SelectedItem as Cancion).Titulo;
- }
- else
- {
- songsListView.SelectedIndex = 0;
- titulo = (songsListView.Items[0] as Cancion).Titulo;
- }
- }
- else
- {
- if (playlistListView.SelectedItem != null)
- {
- titulo = (playlistListView.SelectedItem as Cancion).Titulo;
- }
- else
- {
- playlistListView.SelectedIndex = 0;
- titulo = (playlistListView.Items[0] as Cancion).Titulo;
- }
- }
- try
- {
- sql = "SELECT ubicacion FROM canciones WHERE titulo = '" + titulo + "'";
- cmd = new MySqlCommand(sql, conn);
- conn.Open();
- reader = cmd.ExecuteReader();
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- ubicacion = reader.GetString(0);
- }
- }
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- if (ubicacion != "")
- {
- try
- {
- player.Open(new Uri(ubicacion));
- player.Play();
- while (!player.NaturalDuration.HasTimeSpan) ;
- timeSlider.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;
- totalTimeLabel.Content = player.NaturalDuration.TimeSpan.ToString(@"m\:ss");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- private void stopButton_Click(object sender, RoutedEventArgs e)
- {
- player.Stop();
- }
- private void skipPreviousButton_Click(object sender, RoutedEventArgs e)
- {
- if (songsListView.IsVisible) songsListView.SelectedIndex -= 1;
- else playlistListView.SelectedIndex -= 1;
- playButton_Click(sender, e);
- }
- private void skipNextButton_Click(object sender, RoutedEventArgs e)
- {
- if (songsListView.IsVisible) songsListView.SelectedIndex += 1;
- else playlistListView.SelectedIndex += 1;
- playButton_Click(sender, e);
- }
- private void timeSlider_DragCompleted(object sender, RoutedEventArgs e)
- {
- TimeSpan t = TimeSpan.FromSeconds(timeSlider.Value);
- player.Position = t;
- }
- private void volumeSlider_DragCompleted(object sender, RoutedEventArgs e)
- {
- player.Volume = volumeSlider.Value;
- }
- private void ToggleButton_Click(object sender, RoutedEventArgs e)
- {
- if (songsListView.IsVisible)
- {
- songsListView.Visibility = Visibility.Hidden;
- playlistListView.Visibility = Visibility.Visible;
- }
- else
- {
- songsListView.Visibility = Visibility.Visible;
- playlistListView.Visibility = Visibility.Hidden;
- }
- }
- private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- CollectionViewSource.GetDefaultView(songsListView.ItemsSource).Refresh();
- }
- private void librarySearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- CollectionViewSource.GetDefaultView(libraryGrid.ItemsSource).Refresh();
- }
- private void addToPlaylistButton_Click(object sender, RoutedEventArgs e)
- {
- int cancion = 0;
- string titulo = "";
- if (libraryGrid.SelectedItem != null)
- {
- titulo = (libraryGrid.SelectedItem as Cancion).Titulo;
- }
- try
- {
- conn.Open();
- sql = "SELECT id FROM canciones WHERE titulo = '" + titulo + "'";
- cmd = new MySqlCommand(sql, conn);
- cancion = (int)cmd.ExecuteScalar();
- sql = "INSERT INTO cancion_playlist (id_cancion, id_playlist) VALUES ('" + cancion + "', '" + currentPlaylist + "')";
- cmd = new MySqlCommand(sql, conn);
- cmd.ExecuteNonQuery();
- sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
- "FROM canciones, artistas, albums, playlists, cancion_playlist " +
- "WHERE playlists.id = cancion_playlist.id_playlist AND " +
- "cancion_playlist.id_cancion = canciones.id AND " +
- "canciones.id_album = albums.id AND " +
- "canciones.id_artista = artistas.id AND " +
- "playlists.id = '" + currentPlaylist + "'";
- cmd = new MySqlCommand(sql, conn);
- reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- if (reader.NextResult())
- {
- List<Cancion> playlistItems = new List<Cancion>();
- while (reader.Read())
- {
- playlistItems.Add(new Cancion()
- {
- Titulo = reader.GetString(0),
- Genero = reader.GetString(1),
- Artista = reader.GetString(2),
- Album = reader.GetString(3),
- });
- }
- if (playlistItems != null)
- playlistListView.ItemsSource = playlistItems;
- }
- }
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- private void removeFromPlaylistButton_Click(object sender, RoutedEventArgs e)
- {
- int cancion = 0;
- string titulo = "";
- if (libraryGrid.SelectedItem != null)
- {
- titulo = (libraryGrid.SelectedItem as Cancion).Titulo;
- }
- try
- {
- conn.Open();
- sql = "SELECT id FROM canciones WHERE titulo = '" + titulo + "'";
- cmd = new MySqlCommand(sql, conn);
- cancion = (int)cmd.ExecuteScalar();
- sql = "DELETE FROM cancion_playlist WHERE id_cancion = '" + cancion + "' AND id_playlist = '" + currentPlaylist + "'";
- cmd = new MySqlCommand(sql, conn);
- cmd.ExecuteNonQuery();
- sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
- "FROM canciones, artistas, albums, playlists, cancion_playlist " +
- "WHERE playlists.id = cancion_playlist.id_playlist AND " +
- "cancion_playlist.id_cancion = canciones.id AND " +
- "canciones.id_album = albums.id AND " +
- "canciones.id_artista = artistas.id AND " +
- "playlists.id = '" + currentPlaylist + "'";
- cmd = new MySqlCommand(sql, conn);
- reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- if (reader.NextResult())
- {
- List<Cancion> playlistItems = new List<Cancion>();
- while (reader.Read())
- {
- playlistItems.Add(new Cancion()
- {
- Titulo = reader.GetString(0),
- Genero = reader.GetString(1),
- Artista = reader.GetString(2),
- Album = reader.GetString(3),
- });
- }
- if (playlistItems != null)
- playlistListView.ItemsSource = playlistItems;
- }
- }
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- private async void newPlaylistButton_Click(object sender, RoutedEventArgs e)
- {
- var result = await this.ShowInputAsync("Crear Playlist", "Nombre:");
- if (result == null)
- return;
- try
- {
- sql = "INSERT INTO playlists (nombre) VALUES ('" + result + "')";
- cmd = new MySqlCommand(sql, conn);
- conn.Open();
- cmd.ExecuteNonQuery();
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- if (songsListView.Visibility == Visibility.Visible)
- {
- songsListView.Visibility = Visibility.Hidden;
- playlistListView.Visibility = Visibility.Visible;
- }
- }
- private void showPlaylistComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- string nombre = "";
- if (showPlaylistComboBox.SelectedItem != null)
- {
- nombre = (showPlaylistComboBox.SelectedItem as Playlist).Nombre;
- }
- try
- {
- conn.Open();
- sql = "SELECT id FROM playlists WHERE nombre = '" + nombre + "'";
- cmd = new MySqlCommand(sql, conn);
- currentPlaylist = (int)cmd.ExecuteScalar();
- sql = "SELECT canciones.titulo, canciones.genero, artistas.nombre, albums.nombre " +
- "FROM canciones, artistas, albums, playlists, cancion_playlist " +
- "WHERE playlists.id = cancion_playlist.id_playlist AND " +
- "cancion_playlist.id_cancion = canciones.id AND " +
- "canciones.id_album = albums.id AND " +
- "canciones.id_artista = artistas.id AND " +
- "playlists.id = '" + currentPlaylist + "'";
- cmd = new MySqlCommand(sql, conn);
- reader = cmd.ExecuteReader();
- if (reader.HasRows)
- {
- List<Cancion> playlistItems = new List<Cancion>();
- while (reader.Read())
- {
- playlistItems.Add(new Cancion()
- {
- Titulo = reader.GetString(0),
- Genero = reader.GetString(1),
- Artista = reader.GetString(2),
- Album = reader.GetString(3),
- });
- }
- if (playlistItems != null)
- playlistListView.ItemsSource = playlistItems;
- }
- else
- {
- playlistListView.ItemsSource = null;
- }
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- }
- private void Expander_Expanded(object sender, RoutedEventArgs e)
- {
- Application.Current.MainWindow.MaxWidth = 2000;
- Application.Current.MainWindow.Width = 900;
- }
- private void Expander_Collapsed(object sender, RoutedEventArgs e)
- {
- Application.Current.MainWindow.MaxWidth = 366;
- }
- //private void newSongButton_Click(object sender, RoutedEventArgs e)
- //{
- // OpenFileDialog fd = new OpenFileDialog();
- // fd.Filter = "Archivos de Audio (*.mp3)";
- // if (fd.ShowDialog() == true)
- // {
- // TagLib.File f = TagLib.File.Create(fd.FileName);
- // try
- // {
- // sql = "INSERT INTO album (nombre) VALUES ('" + result + "')";
- // cmd = new MySqlCommand(sql, conn);
- // conn.Open();
- // cmd.ExecuteNonQuery();
- // }
- // catch (MySqlException ex)
- // {
- // MessageBox.Show(ex.Message);
- // }
- // finally
- // {
- // conn.Close();
- // }
- // }
- //}
- }
- }
Add Comment
Please, Sign In to add comment