//Reproductor MP3 con OCX de Windows Media Player 9 en modo invisible

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MP3_Player
{
    /// <summary>
    /// Descripción breve de Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private AxWMPLib.AxWindowsMediaPlayer ocxPlayer;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.ToolTip toolTip1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.Button btAbrir;
        private System.Windows.Forms.Button btCerrar;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button button1;
        internal System.Windows.Forms.Label lbTitulo;
        private System.ComponentModel.IContainer components;

        public Form1()
        {
            //
            // Necesario para admitir el Diseñador de Windows Forms
            //
            InitializeComponent();

            //
            // TODO: agregar código de constructor después de llamar a InitializeComponent
            //
        }

        /// <summary>
        /// Limpiar los recursos que se estén utilizando.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Código generado por el Diseñador de Windows Forms
        /// <summary>
        /// Método necesario para admitir el Diseñador. No se puede modificar
        /// el contenido del método con el editor de código.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.ocxPlayer = new AxWMPLib.AxWindowsMediaPlayer();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.lbTitulo = new System.Windows.Forms.Label();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.btAbrir = new System.Windows.Forms.Button();
            this.btCerrar = new System.Windows.Forms.Button();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.ocxPlayer)).BeginInit();
            this.SuspendLayout();
            // 
            // ocxPlayer
            // 
            this.ocxPlayer.Enabled = true;
            this.ocxPlayer.Location = new System.Drawing.Point(16, 24);
            this.ocxPlayer.Name = "ocxPlayer";
            this.ocxPlayer.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("ocxPlayer.OcxState")));
            this.ocxPlayer.Size = new System.Drawing.Size(296, 248);
            this.ocxPlayer.TabIndex = 2;
            this.ocxPlayer.TabStop = false;
            this.ocxPlayer.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(this.ocxPlayer_PlayStateChange);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(352, 32);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(88, 32);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            // 
            // pictureBox2
            // 
            this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
            this.pictureBox2.Location = new System.Drawing.Point(336, 88);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(120, 48);
            this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox2.TabIndex = 3;
            this.pictureBox2.TabStop = false;
            // 
            // lbTitulo
            // 
            this.lbTitulo.BackColor = System.Drawing.SystemColors.Control;
            this.lbTitulo.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.lbTitulo.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lbTitulo.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lbTitulo.Location = new System.Drawing.Point(8, 296);
            this.lbTitulo.Name = "lbTitulo";
            this.lbTitulo.Size = new System.Drawing.Size(456, 16);
            this.lbTitulo.TabIndex = 6;
            this.lbTitulo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.toolTip1.SetToolTip(this.lbTitulo, "Información sobre el clip");
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.RestoreDirectory = true;
            // 
            // btAbrir
            // 
            this.btAbrir.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btAbrir.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btAbrir.Location = new System.Drawing.Point(328, 248);
            this.btAbrir.Name = "btAbrir";
            this.btAbrir.Size = new System.Drawing.Size(64, 32);
            this.btAbrir.TabIndex = 0;
            this.btAbrir.TabStop = false;
            this.btAbrir.Text = "Abrir";
            this.btAbrir.Click += new System.EventHandler(this.btAbrir_Click);
            // 
            // btCerrar
            // 
            this.btCerrar.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btCerrar.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btCerrar.Location = new System.Drawing.Point(400, 248);
            this.btCerrar.Name = "btCerrar";
            this.btCerrar.Size = new System.Drawing.Size(64, 32);
            this.btCerrar.TabIndex = 1;
            this.btCerrar.TabStop = false;
            this.btCerrar.Text = "Cerrar";
            this.btCerrar.Click += new System.EventHandler(this.btCerrar_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Location = new System.Drawing.Point(8, 8);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(312, 272);
            this.groupBox1.TabIndex = 5;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Reproductor de Windows Media";
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Location = new System.Drawing.Point(328, 16);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(136, 144);
            this.panel1.TabIndex = 3;
            // 
            // button1
            // 
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button1.Location = new System.Drawing.Point(328, 176);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(136, 56);
            this.button1.TabIndex = 4;
            this.button1.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(470, 320);
            this.Controls.Add(this.lbTitulo);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.btCerrar);
            this.Controls.Add(this.btAbrir);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.ocxPlayer);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.panel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "MP3 | M3U | AVI (en C#)";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.ocxPlayer)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// Punto de entrada principal de la aplicación.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            this.Top = 80;
            this.Left = 80;
            this.toolTip1.SetToolTip (this.pictureBox2,  "Utiliza el control OCX de \nWIndows Media Player 9");
            this.button1.Enabled = false;
            //this.ControlBox = false;
        }

        //procedimiento para abrir un archivo
        private void abrirArchivo()
        {
            try
            {
                this.openFileDialog1 = new OpenFileDialog();
                this.openFileDialog1.Filter = "Listas de reproducción (*.wpl)|*.wpl|Listas de reproducción (*.m3u)|*.m3u|Canciones MP3 (*.mp3)|*.mp3|Archivos de vídeo (*.avi)|*.avi";
                this.openFileDialog1.Title = "Canciones MP3 o Listas M3U";
                if (openFileDialog1.ShowDialog()==DialogResult.OK)
                {
                    //el clip reproducido es el archivo elegido en el diálogo
                    this.ocxPlayer.URL = this.openFileDialog1.FileName;
                }
            }
            catch (Exception pollo)
            {
                MessageBox.Show ("Error producido: " + pollo.Message, "Error del programa");
            }
        }

        private void btAbrir_Click(object sender, System.EventArgs e)
        {
            abrirArchivo();
        }

        private void btCerrar_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        /*El método Media.getItemInfo devuelve el valor del atributo
         * especificado para el medio actual. Los atributos más utilizados son:
         * Author / BitRate / CurrentBitrate / Duration / FileType / MediaType
         * / SourceURL / Title / UserPlayCount*/

        //al cambiar el estado del reproductor (de play a stop, etc)
        private void ocxPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            this.lbTitulo.Text = "";
            string cadena;
            /*seleccionar los 3 últimos caracteres del nombre de archivo para
                 * saber su extensión, Math.Max se usa para evitar errores si
                 * la cadena mide menos de 3 caracteres (índice negativo)*/
            cadena = ocxPlayer.URL.Substring(Math.Max(0, ocxPlayer.URL.Length - 3));
            if (cadena != "avi")
            {
                this.lbTitulo.Text = this.ocxPlayer.currentMedia.getItemInfo("Title") + "  @  " + this.ocxPlayer.currentMedia.getItemInfo("Author") + "  @  " + this.ocxPlayer.currentMedia.durationString;
            }
            if (cadena == "avi")
            {
                this.lbTitulo.Text = this.ocxPlayer.currentMedia.getItemInfo("Title") + "  @  " + "Archivo de vídeo" + "  @  " + this.ocxPlayer.currentMedia.durationString;
            }
        }
    
    }
}