+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

Thread: Tutorial: Playing MP3 files with C#

  1. #1
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Cool Tutorial: Playing MP3 files with C#

    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:

    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:

    Code:
    [DllImport("winmm.dll")]
    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 Tutorial: Playing MP3 files with C#-running-program-2-.jpg   Tutorial: Playing MP3 files with C#-playing-video-2-.jpg  
    Attached Files
    Last edited by ArekBulski; 09-03-2009 at 04:25 AM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Tutorial: Playing MP3 files with C#

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

  3. #3
    Programming Professional Parabola will become famous soon enough Parabola will become famous soon enough Parabola's Avatar
    Join Date
    Jul 2009
    Location
    Texas
    Age
    25
    Posts
    300
    Blog Entries
    3

    Re: Tutorial: Playing MP3 files with C#

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

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: Tutorial: Playing MP3 files with C#

    More +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #5
    Programming Professional so1i is an unknown quantity at this point so1i's Avatar
    Join Date
    Sep 2009
    Posts
    229

    Re: Tutorial: Playing MP3 files with C#

    Nice tutorial, very helpful! +rep

  6. #6
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    Re: Tutorial: Playing MP3 files with C#

    very nice +rep
    The owls are not what they seem...

  7. #7
    Newbie diedo is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    3

    Re: Tutorial: Playing MP3 files with C#

    yeah very nice tutorial

  8. #8
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Tutorial: Playing MP3 files with C#

    Thank you all, guys.

  9. #9
    Newbie Darkco is an unknown quantity at this point Darkco's Avatar
    Join Date
    Feb 2008
    Location
    Holland
    Age
    17
    Posts
    13

    Re: Tutorial: Playing MP3 files with C#

    Very nice tutorial indeed
    But can you please explain this part of the code I dont fully understand it yet ...
    Code:
    private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
    Last edited by Darkco; 01-11-2010 at 05:02 PM. Reason: grammar

  10. #10
    The Crazy One TkTech will become famous soon enough TkTech's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Age
    18
    Posts
    1,549
    Blog Entries
    1

    Re: Tutorial: Playing MP3 files with C#

    lol @ Eureka Seven

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Some Basic Linux Commands, For Beginners...
    By Onur in forum Linux Tutorials, Guides and Tips
    Replies: 5
    Last Post: 10-12-2009, 02:07 AM
  2. C# Tutorial: Writing Text Files
    By Xav in forum CSharp Tutorials
    Replies: 46
    Last Post: 07-28-2009, 08:18 AM
  3. Uploading unknown number of files using php & JS
    By amrosama in forum PHP Tutorials
    Replies: 4
    Last Post: 01-08-2009, 09:48 AM
  4. Replies: 0
    Last Post: 12-14-2008, 05:20 PM
  5. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 03:05 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts