Este vez les traigo un reproductor de musica y peliculas que hice en C# usando WPF con las siguientes opciones :
- Reproduce musica y videos a pantalla completa
- Soporta Drag and Drop para reproducir canciones y videos
- Pueden subir volumen y poner la posicion que quieran
- Tienen opcion para repetir una cancion o reproducir una carpeta entera automaticamente
- Pueden poner mute
* Formatos de musica soportados : mp3,m4a,wma
* Formato de videos soportados : avi,mp4,flv,mkv,wmv,mpg
Una imagen :
El codigoCódigo (csharp) [Seleccionar]
// DH Player 1.0
// (C) Doddy Hackman 2015
// Credits :
// Based on : MP3 Happy Hard Core Player V 1.0 By Steel Karpov
// Thanks to Steel Karpov
using System;
using System.Collections.Generic;
using System.Text;
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 Microsoft.Win32;
using System.IO;
using System.Windows.Threading;
using System.Text.RegularExpressions;
namespace DH_Player
{
/// <summary>
/// Lógica de interacción para MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer control_del_tiempo;
bool escribiendo;
bool enalgo;
bool repeat;
bool automatic;
bool pantalla_completa = false;
public MainWindow()
{
InitializeComponent();
control_del_tiempo = new DispatcherTimer();
control_del_tiempo.Interval = TimeSpan.FromSeconds(1);
control_del_tiempo.Tick += new EventHandler(timer_Tick);
escribiendo = false;
}
private void timer_Tick(object sender, EventArgs e)
{
if (!escribiendo)
{
linea.Value = player.Position.TotalSeconds;
}
}
private void retroceder_MouseUp(object sender, MouseButtonEventArgs e)
{
if (lista.SelectedIndex != -1)
{
cancion_anterior();
}
}
private void play_MouseUp(object sender, MouseButtonEventArgs e)
{
if (lista.SelectedIndex != -1)
{
enalgo = true;
player.Play();
}
}
private void pause_MouseUp(object sender, MouseButtonEventArgs e)
{
if (lista.SelectedIndex != -1)
{
player.Pause();
enalgo = false;
}
}
private void avanzar_MouseUp(object sender, MouseButtonEventArgs e)
{
if (lista.SelectedIndex != -1)
{
cancion_siguiente();
}
}
private void nombres_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (lista.SelectedIndex != -1)
{
enalgo = false;
start_music();
}
}
private void start_music()
{
if (enalgo == true)
{
player.Play();
}
else
{
ListBoxItem ruta1 = (ListBoxItem)lista.SelectedItem;
string ruta = ruta1.Tag.ToString();
player.Position = TimeSpan.Zero;
player.LoadedBehavior = MediaState.Manual;
player.UnloadedBehavior = MediaState.Stop;
player.Source = new Uri(ruta);
linea.Value = 0;
player.Play();
}
}
private void cancion_siguiente()
{
enalgo = false;
try
{
if (lista.SelectedIndex + 1 < lista.Items.Count)
{
lista.SelectedItem = lista.Items.GetItemAt(lista.SelectedIndex + 1);
start_music();
}
}
catch
{
//
}
}
private void cancion_anterior()
{
enalgo = false;
try
{
if (lista.SelectedIndex - 1 < lista.Items.Count)
{
lista.SelectedItem = lista.Items.GetItemAt(lista.SelectedIndex - 1);
start_music();
}
}
catch
{
//
}
}
private void volumen_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
try
{
player.Volume = volumen.Value;
}
catch
{
//
}
}
private void player_MouseDown(object sender, MouseButtonEventArgs e)
{
int width_screen = 1400;
int height_screen = 800;
int width_original = 483;
int height_original = 372;
if (e.ClickCount == 2 && pantalla_completa == false)
{
player.MinHeight = height_screen;
player.MinWidth = width_screen;
volumen.Visibility = Visibility.Hidden;
linea.Visibility = Visibility.Hidden;
play.Visibility = Visibility.Hidden;
pause.Visibility = Visibility.Hidden;
avanzar.Visibility = Visibility.Hidden;
retroceder.Visibility = Visibility.Hidden;
contenedor1.Visibility = Visibility.Hidden;
contenedor2.Visibility = Visibility.Hidden;
lista.Visibility = Visibility.Hidden;
menu1.Visibility = Visibility.Hidden;
progreso.Visibility = Visibility.Hidden;
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
pantalla_completa = true;
}
else
{
if (e.ClickCount == 2 && pantalla_completa == true)
{
player.MinHeight = height_original;
player.MinWidth = width_original;
volumen.Visibility = Visibility.Visible;
linea.Visibility = Visibility.Visible;
play.Visibility = Visibility.Visible;
pause.Visibility = Visibility.Visible;
avanzar.Visibility = Visibility.Visible;
retroceder.Visibility = Visibility.Visible;
contenedor1.Visibility = Visibility.Visible;
contenedor2.Visibility = Visibility.Visible;
lista.Visibility = Visibility.Visible;
menu1.Visibility = Visibility.Visible;
progreso.Visibility = Visibility.Visible;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.WindowState = WindowState.Normal;
pantalla_completa = false;
}
}
}
private void player_MediaOpened(object sender, RoutedEventArgs e)
{
enalgo = true;
if (player.NaturalDuration.HasTimeSpan)
{
TimeSpan ts = player.NaturalDuration.TimeSpan;
linea.Maximum = ts.TotalSeconds;
linea.SmallChange = 1;
}
control_del_tiempo.Start();
}
private void player_MediaEnded(object sender, RoutedEventArgs e)
{
linea.Value = 0;
enalgo = false;
if (repeat == true)
{
start_music();
}
if (automatic == true)
{
cancion_siguiente();
}
}
private void linea_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
escribiendo = true;
}
private void linea_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
escribiendo = false;
player.Position =
TimeSpan.FromSeconds(linea.Value);
}
private void linea_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
player.Position = TimeSpan.FromSeconds(linea.Value);
}
private void linea_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
try
{
string tiempo = TimeSpan.FromSeconds(linea.Value).ToString();
int cantidad = (int)player.NaturalDuration.TimeSpan.TotalSeconds;
TimeSpan tiempo_final = TimeSpan.FromSeconds(cantidad);
Match regex = Regex.Match(tiempo, @"(.*)\.(.*)", RegexOptions.IgnoreCase);
if (regex.Success)
{
progreso.Content = regex.Groups[1].Value + " / " + tiempo_final;
}
}
catch
{
//
}
}
private void nombres_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Copy;
}
else
{
e.Effects = DragDropEffects.None;
}
}
private void nombres_Drop(object sender, DragEventArgs e)
{
List<string> archivos = new List<string>((string[])e.Data.GetData(DataFormats.FileDrop));
foreach (string archivo in archivos)
{
string nombre = System.IO.Path.GetFileNameWithoutExtension(archivo);
string extension = System.IO.Path.GetExtension(archivo);
if (extension == ".mp3" || extension == ".avi" || extension==".m4a" || extension==".wma" || extension==".mp4" || extension==".flv" || extension==".mkv" || extension==".wmv" || extension==".mpg")
{
ListBoxItem item1 = new ListBoxItem();
item1.Content = nombre;
item1.Tag = archivo;
lista.Items.Add(item1);
}
}
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "MP3 (.mp3)|*.mp3|M4A (.m4a)|*.m4a|WMA (.wma)|*.m4a";
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
openFileDialog1.Multiselect = true;
openFileDialog1.Title = "Select Song";
Nullable<bool> result = openFileDialog1.ShowDialog();
if (result == true)
{
string[] archivos = openFileDialog1.FileNames;
foreach (string archivo in archivos)
{
string nombre = System.IO.Path.GetFileNameWithoutExtension(archivo);
ListBoxItem item1 = new ListBoxItem();
item1.Content = nombre;
item1.Tag = archivo;
lista.Items.Add(item1);
}
}
}
private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "MP3 (.mp3)|*.mp3|M4A (.m4a)|*.m4a|WMA (.wma)|*.m4a";
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
openFileDialog1.Multiselect = true;
openFileDialog1.Title = "Select Song";
Nullable<bool> result = openFileDialog1.ShowDialog();
if (result == true)
{
string directorio = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
string[] archivos = Directory.GetFiles(directorio);
foreach (string archivo in archivos)
{
string extension = System.IO.Path.GetExtension(archivo);
if (extension == ".mp3" || extension == ".m4a" || extension==".wma")
{
string nombre = System.IO.Path.GetFileNameWithoutExtension(archivo);
ListBoxItem item1 = new ListBoxItem();
item1.Content = nombre;
item1.Tag = archivo;
lista.Items.Add(item1);
}
}
}
}
private void MenuItem_Click_2(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "avi (.avi)|*.avi|mp4 (.mp4)|*.mp4|flv (.flv)|*.flv|mkv (.mkv)|*.mkv|wmv (.wmv)|*.wmv|mpg (.mpg)|*.mpg";
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
openFileDialog1.Multiselect = true;
openFileDialog1.Title = "Select Video";
Nullable<bool> result = openFileDialog1.ShowDialog();
if (result == true)
{
string[] archivos = openFileDialog1.FileNames;
foreach (string archivo in archivos)
{
string nombre = System.IO.Path.GetFileNameWithoutExtension(archivo);
ListBoxItem item1 = new ListBoxItem();
item1.Content = nombre;
item1.Tag = archivo;
lista.Items.Add(item1);
}
}
}
private void MenuItem_Click_3(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "avi (.avi)|*.avi|mp4 (.mp4)|*.mp4|flv (.flv)|*.flv|mkv (.mkv)|*.mkv|wmv (.wmv)|*.wmv|mpg (.mpg)|*.mpg";
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
openFileDialog1.Multiselect = true;
openFileDialog1.Title = "Select Videos";
Nullable<bool> result = openFileDialog1.ShowDialog();
if (result == true)
{
string directorio = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
string[] archivos = Directory.GetFiles(directorio);
foreach (string archivo in archivos)
{
string extension = System.IO.Path.GetExtension(archivo);
if (extension == ".avi" || extension==".mp4" || extension ==".flv" || extension==".mkv" || extension ==".wmv" || extension==".mpg")
{
string nombre = System.IO.Path.GetFileNameWithoutExtension(archivo);
ListBoxItem item1 = new ListBoxItem();
item1.Content = nombre;
item1.Tag = archivo;
lista.Items.Add(item1);
}
}
}
}
private void MenuItem_Click_4(object sender, RoutedEventArgs e)
{
repeat = true;
}
private void MenuItem_Click_5(object sender, RoutedEventArgs e)
{
repeat = false;
}
private void MenuItem_Click_6(object sender, RoutedEventArgs e)
{
automatic = true;
}
private void MenuItem_Click_7(object sender, RoutedEventArgs e)
{
automatic = false;
}
private void MenuItem_Click_10(object sender, RoutedEventArgs e)
{
player.IsMuted = true;
}
private void MenuItem_Click_11(object sender, RoutedEventArgs e)
{
player.IsMuted = false;
}
private void MenuItem_Click_8(object sender, RoutedEventArgs e)
{
MessageBox.Show("Written By Doddy Hackman in the summer of 2015");
}
private void MenuItem_Click_9(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void MenuItem_Click_12(object sender, RoutedEventArgs e)
{
lista.Items.Clear();
}
}
}
// The End ?
Si quieren bajar el programa lo pueden hacer de aca :
SourceForge.
Github.
Eso es todo.