Thread: Playing Music in C#

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Playing Music in C#

    is there a way to play music in a C# app? ive been searching forums and code sites for a while and haven't been able to find something on it.
    thanks

  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187

    Try -

    Code:
    [DllImport("winmm.dll")] 
          public static extern long PlaySound(String lpszname, long hModule, long dwFlags);

    Now, do the CBoard a favour,
    Post some code when it works


  3. #3
    Unregistered
    Guest
    Yeah I heard that they forgot to add sound to Vs.net, but than you can allways export Win32 functionality in your managed C# project. I think that you need to add the dll to the assembly or something like that from the command line though, but I could be wrong. The linker is called al.exe, so type al on the command prompt to see all the options.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Are you talking about your basic 84 note PC-Speaker music, or are you talking full blown, Stereo, CD, WIN32 sound?

  5. #5
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    I think he means Rich Windows audio stuff like .wav, .mp3

    If you know know to play either could
    you post an example ?

  6. #6
    Registered User Esss's Avatar
    Join Date
    Aug 2001
    Posts
    133
    Quite the easiest method is to use the ActiveMovie (quartz.dll) COM library.
    Add a reference to the ActiveMovie COM library with Add Reference. This will generate an RCW (runtime callable wrapper) around the library, and will put it in the QuartzTypeLib namespace. Then:
    PHP Code:
    QuartzTypeLib.FilgraphManagerClass fm = new QuartzTypeLib.FilgraphManagerClass();

    fm.RenderFile("D:\\\\My Music\\\\Whitesnake - Is This Love.mp3");
    fm.Run(); 
    The current position is available from the CurrentPosition property, and you can check to see if it's finished by comparing this with the StopPosition property.
    PHP Code:
    do
    {
        
    System.Console.Write(String.Format("\r{0}", (int)fm.CurrentPosition));
    }
    while (
    fm.CurrentPosition fm.StopTime);
    fm.Stop(); 
    Ess
    Like a rat in a maze who says,
    "Watch me choose my own direction"
    Are you under the illusion
    The path is winding your way?
    - Rush

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Still this isn't .net. It's running unmanaged code within a managed assembly. Is there any music playing functionality in the .net BCL?

  8. #8
    Registered User Esss's Avatar
    Join Date
    Aug 2001
    Posts
    133
    > Still this isn't .net. It's running unmanaged code within a managed assembly.

    Of course it is. So is anything you do that interacts with the user. Ultimately it always comes down to unmanaged code. Display a window, and some unmanaged code (rather a lot of it, actually) is used to show that window. Or did you have some other means in mind to address the soundcard?

    There are no classes within the .NET Framework for sound of any kind.
    Ess
    Like a rat in a maze who says,
    "Watch me choose my own direction"
    Are you under the illusion
    The path is winding your way?
    - Rush

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Yes, I can tell that the framework makes calls to Win32 when I use the cordbg command line debugger utility. Before optimizing the JIT's it calls Win32 about 3700 times for a hello world program. After optimization it ends up at around 355 or approximately 10 percent.

    The idea I'm wondering about however is the frameworks capability to allow the programmer to deploy much easier. Simply cut and paste the assembly, however does having too many unmanaged modules screw that up? Also with the assembly apparently you have dll versioning, but does unmanaged modules interfere with that too? Why doesn't the Framework Class Library have a namespace which deals with music files?

  10. #10
    Registered User Esss's Avatar
    Join Date
    Aug 2001
    Posts
    133
    > The idea I'm wondering about however is the frameworks capability to allow the programmer to deploy much easier. Simply cut and paste the assembly, however does having too many unmanaged modules screw that up?

    Not if you can rely on the unmanaged modules being there, which is of course possible with the ActiveMovie library.

    > Also with the assembly apparently you have dll versioning, but does unmanaged modules interfere with that too?

    Obviously, using COM components implies you wind up using the COM versioning model. Which is not terribly robust. However, it doesn't have any effect on the versioning of your own assemblies.

    > Why doesn't the Framework Class Library have a namespace which deals with music files?

    Probably because the rest of the .NET Framework took up all the available time. There are rumours of 'managed DirectX', but when it's this easy (and reliable), why bother waiting?
    Ess
    Like a rat in a maze who says,
    "Watch me choose my own direction"
    Are you under the illusion
    The path is winding your way?
    - Rush

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is AOL music crazy?
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-24-2006, 07:24 PM
  2. Playing music
    By adr in forum Windows Programming
    Replies: 13
    Last Post: 12-31-2005, 03:23 PM
  3. playing music in C++
    By dutch's finest in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2005, 05:57 PM
  4. Playing music
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 06-25-2003, 09:09 AM
  5. Playing Music
    By Quantrizi in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2002, 07:47 PM