How do I implement sounds like music in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MattBergen
    New Member
    • May 2010
    • 3

    How do I implement sounds like music in C#

    Hi I was wondering how I could use sounds in a C# program, like mp3's, and how would I run the program and the music at the same time? I use the 2005 version of C#.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    For cases like this, google is your friend. General searches of the form "<language> <task>" typically turn up excellent resources. Give it a try, a few great links came up near the top that should get you on the right track.

    As for playing it at the same as the program runs, I believe it's threaded. The music playing is processed by a different thread than the one your program executes on.

    Good luck!
    Last edited by GaryTexmo; May 31 '10, 09:13 PM. Reason: Re-added a portion of the removed text which provides the means for the OP to resolve their problem

    Comment

    • MattBergen
      New Member
      • May 2010
      • 3

      #3
      i found a way of doing it but i keep getting a error for this code
      Code:
         private void label1_Click(object sender, EventArgs e)
              {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer();
                player.SoundLocation =   
                @"C:/Users/Ussssssssssser/Desktop/sounds/elvis.wav";
      
                  player.Play();
              }
      it highlights player.Play(); and says
      InvalidOperatio nException was unhandled
      Sound API only supports playing PCM wave files.

      and i dont know how to fix it, im really new at C#

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Originally posted by MattBergen
        i found a way of doing it but i keep getting a error for this code
        private void label1_Click(ob ject sender, EventArgs e)
        {
        System.Media.So undPlayer player = new System.Media.So undPlayer();
        player.SoundLoc ation =
        @"C:/Users/Ussssssssssser/Desktop/sounds/elvis.wav";

        player.Play();
        }
        it highlights player.Play(); and says
        InvalidOperatio nException was unhandled
        Sound API only supports playing PCM wave files.

        and i dont know how to fix it, im really new at C#
        The exception message is pretty much all you need to know. Where did you get your wave file? Perhaps try another... maybe record one of your own with sound recorder to test it out. If the one you made yourself works, that's a pretty good indication that the other file is no good. If it doesn't work, something else might be going wrong.

        Hmm, I did some googling around (again, you should really do this...) and it sounds like there might be a problem with streaming sound content over the web. Are you trying to do this, or is this a local application? I'm at home right now and recently got a new computer so I don't have VS installed. I'll try making a test project tomorrow when I'm at work, but I'd still recommend you try the above suggestions (a wave file you created, and googling around for more information).

        I'll try to get back to you tomorrow.

        Comment

        • MattBergen
          New Member
          • May 2010
          • 3

          #5
          Hey thanks for the help I got the sound to work but now I’m wondering how I could play the sounds and the main program at the same time. I’ve looked up threading but don’t understand it fully and was wondering if there is an easier way?

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Oh, right on, glad you got it working.

            Well, if you start it in a different thread it will play with the main program at the same time. I don't really understand what you're wanting. Your main program can still control the thread the player is running on so you can start/stop the player based on responses from the main GUI.

            To be honest, I thought the player was already threaded on it's own... is this not the case? Can you describe the behaviour? I'm at home looking after my cat who just had surgery so I can't do up a test program, but I can still try to help if you like.

            Comment

            Working...