Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Tutorial: Playing MP3 files with C#

  • Please log in to reply
26 replies to this topic

#1 ArekBulski


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 01 September 2009 - 11:19 PM

Hello to everyone! This tutorial will explain how to play MP3 files as songs, without any external assemblies or SDKs. I dedicate this tutorial to Thomas Morgan, a new but not unwelcomed member of our forum. And to all who seek the power of .NET. :)

There are several technologies available

There are multiple approaches to playing audio multimedia as it seems. I could try to embed a Windows Media Player using one of the SDKs. But that would be something completely new to me. I decided to leave it for another time.

I could use Managed DirectX, too. This one is very powerful and easy to use, using the AudioVideoPlayback namespace. I very much like Managed DirectX, but not every system will be able to run my program. You need to install both .NET Framework and DirectX in correct order to be able to run it. This becomes a problem, from wider perspective.

This tutorial will be using mciSendString WinAPI function, through P/Invoke. Therefore it won't run on Mono. Honestly I hate WinAPI functions, but if that is the easiest way then let's do it.

Playing MP3 songs using mciSendString function


The whole code is just three methods long. Notice that user has to select a song to be able to press Play button. And he needs to play it to be able to stop it. It will easily prevent user from clicking buttons he should not click yet. Here is a simple file selection dialog code:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
    FilenameTextbox.Text = openFileDialog1.FileName;

    button3.Enabled = true; //So you dont play no file. lol

Then we need to add a P/Invoke declaration to main form's class. Afterwards it is just calling the method few times. If Windows Media Player is already playing a song then they will overlap. No exception but what noise heh. Here is the P/Invoke and the code playing and stopping the song:

private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);

private void button3_Click(object sender, EventArgs e)
    mciSendString("open \"" + FilenameTextbox.Text + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
    mciSendString("play MediaFile", null, 0, IntPtr.Zero);

    button3.Enabled = false;
    button2.Enabled = true;

private void button2_Click(object sender, EventArgs e)
    mciSendString("close MediaFile", null, 0, IntPtr.Zero);

    button2.Enabled = false;
    button3.Enabled = true;

If you are still not satisfied then here is an additional joke. If you select a video then a new window will pop up and display it. Now I'm laughing at myself, lol.


There is nothing as worthy as comments

Thanks to everyone reading my tutorial. Any comments and +rep are welcomed. Hope you enjoyed reading. :)

Attached Thumbnails

  • playing video (2).jpg
  • running program (2).jpg

Attached Files

Edited by ArekBulski, 03 September 2009 - 01:25 AM.

  • 3

#2 Guest_Jordan_*

  • Guest

Posted 02 September 2009 - 04:11 AM

Very cool tutorial! I've used this namespace before in prior projects. It works and is indeed very powerful. +rep
  • 0

#3 Parabola


    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 221 posts

Posted 02 September 2009 - 06:49 AM

Time to make my own media player.... lol Very nice, +rep
  • 0
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.

#4 WingedPanther73


    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 September 2009 - 01:05 PM

More +rep :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

#5 so1i


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 17 September 2009 - 06:10 PM

Nice tutorial, very helpful! +rep
  • 0

#6 debtboy


    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 499 posts

Posted 17 September 2009 - 06:30 PM

very nice +rep
  • 0

#7 diedo


    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 24 September 2009 - 03:34 AM

yeah very nice tutorial
  • 0

#8 ArekBulski


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 24 September 2009 - 01:33 PM

Thank you all, guys. :)
  • 0

#9 Darkco


    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 11 January 2010 - 01:19 PM

Very nice tutorial indeed :)
But can you please explain this part of the code I dont fully understand it yet ...
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);

Edited by Darkco, 11 January 2010 - 03:02 PM.

  • 0

#10 TkTech


    The Crazy One

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1144 posts
  • Location:Ottawa, Ontario

Posted 11 January 2010 - 02:12 PM

lol @ Eureka Seven
  • 0
Helpful CODECALL Links: Join Us, Guidelines, FAQ, Post a Tutorial

#11 gokuajmes


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 22 January 2010 - 01:01 AM

very well done.Good tut.
How do i + Rep U [ i am new over here ]
  • 0

#12 hampustagerud


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 51 posts
  • Location:Los Angeles
  • Programming Language:Java, C#, PHP, Python, JavaScript, ActionScript
  • Learning:C, C++, Objective-C, Assembly

Posted 29 March 2010 - 04:55 AM

Very nice tutorial! Just was I was looking for!! +rep (Y)
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download