SIGN UP MEMBER LOGIN:    
ARTICLE

Simple MP3 Player

Posted by Kapil Soni Articles | Windows Forms C# September 22, 2008
The attached source code with this article is a simple MP3 player written in C# and Visual Studio 2008.
Reader Level:
Download Files:
 

The attached project is a simple MP3 player. The UI looks like following:


Figure 1.

In the above UI, the Browse button let you browse an MP3 file and plays it.

The source code of the Forum class is listed here:

public partial class Form1 : Form
 {
         OpenFileDialog ofd = new OpenFileDialog();
         StringBuilder buffer = new StringBuilder(128);
         int second;
         int minutes;
         string CommandString;
         [DllImport("winmm.dll")]
         private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString,
         int uReturnLength, int hwndCallback);
    Public Form1()
    {
         InitializeComponent();
         txtpath.ReadOnly = true;
    } 

    private void btnplay_Click(object sender, EventArgs e)
    {
         if (ofd.FileName == "")
         {
             if (ofd.ShowDialog() == DialogResult.OK)
             {
                 ofd.Filter = "MP3 Files|*.mp3";
                 CommandString = "open " + "\"" + ofd.FileName + "\"" +
" type MPEGVideo alias Mp3File"
;
                         mciSendString(CommandString, null, 0, 0);
                         CommandString = "play Mp3File";
                         mciSendString(CommandString, null, 0, 0);
                         timer1.Enabled = true;
               }
     } 

         else
         {
                CommandString = "play Mp3File";
                mciSendString(CommandString, null, 0, 0); 
               
                timer1.Enabled = true;
         }
 } 

private
void btnpause_Click(object sender, EventArgs e)
 {
     CommandString = "pause mp3file";
     mciSendString(CommandString, null, 0, 0);
 }

 private void btnbrowse_Click(object sender, EventArgs e)
 {
     ofd.Filter = "Mp3 files |*.mp3";
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         txtpath.Text = ofd.FileName;
         CommandString = "close Mp3File";
         mciSendString(CommandString, null, 0, 0);
         timer1.Enabled = false;
         CommandString = "open " + "\"" + ofd.FileName + "\"" + "
 type MPEGVideo alias Mp3File"
;
         mciSendString(CommandString, null, 0, 0);
 }
 } 

     private void timer1_Tick(object sender, EventArgs e)
     {
         CommandString = "Status Mp3File position";
         mciSendString(CommandString, buffer, 128, 0);
         second = int.Parse(buffer.ToString());
         second = second / 1000;
         minutes = second / 60;
         second = second % 60;
         lbltime.Text = minutes.ToString("00") + ":" +
second.ToString("00");
       }
 }

 

 

 

Login to add your contents and source code to this article
share this article :
post comment
 

i m getting the same exception and still not able to fix the exception input string is not in the correct format in the tick event of the timer1

Posted by devesh agarwal Apr 06, 2011

i have coded this project but i have come one error in run time in this line micSendString(CommandString, null, 0, 0); now talk me how to sole this problem plz help me now

Posted by vikram kadam Feb 11, 2011

ya we have sound control in .Net kindly go for add item in control panel and then add the sound control

Thanks

Posted by Kapil Soni Nov 20, 2010

Thanks for your reply.
i just want to know how we add a music player in the c sharp project?
also can we control the sound also in this project?
help me.
thanks again for your previous help

Posted by rahul kumar Nov 20, 2010

try this project ...it contains all ...

it was my project ....but had a little problem...
it wll not play 320kbps songs....
it can play 128 kbp songs....

http://www.mediafire.com/?im2irmxzy2m

Posted by sravan kumar Nov 18, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor