By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,142 Members | 792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,142 IT Pros & Developers. It's quick & easy.

How do I implement sounds like music in C#

P: 3
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#.
May 31 '10 #1
Share this Question
Share on Google+
5 Replies

GaryTexmo
Expert 100+
P: 1,501
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!
May 31 '10 #2

P: 3
i found a way of doing it but i keep getting a error for this code
Expand|Select|Wrap|Line Numbers
  1.    private void label1_Click(object sender, EventArgs e)
  2.         {
  3.           System.Media.SoundPlayer player = new System.Media.SoundPlayer();
  4.           player.SoundLocation =   
  5.           @"C:/Users/Ussssssssssser/Desktop/sounds/elvis.wav";
  6.  
  7.             player.Play();
  8.         }
it highlights player.Play(); and says
InvalidOperationException was unhandled
Sound API only supports playing PCM wave files.

and i dont know how to fix it, im really new at C#
Jun 1 '10 #3

GaryTexmo
Expert 100+
P: 1,501
i found a way of doing it but i keep getting a error for this 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
InvalidOperationException 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.
Jun 1 '10 #4

P: 3
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?
Jun 1 '10 #5

GaryTexmo
Expert 100+
P: 1,501
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.
Jun 1 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.