473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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#.
May 31 '10 #1
5 3916
GaryTexmo
1,501 Expert 1GB
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
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
1,501 Expert 1GB
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
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
1,501 Expert 1GB
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

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

Similar topics

6
by: Jimnbigd | last post by:
I want to write a game, and sounds will really add to it. Note that I would always make the sounds optional. I hate it when I go to a URL and unexpectedly get sounds or music. I have played...
20
by: gallery | last post by:
Our client insists on having a music loop playing as the visitor travels throughout his new HTML website. We will not be doing this in frames and I would prefer not to have the home page spawn...
87
by: Sony Music CDs install Malware | last post by:
Whether you are a web surfer or a C++ developer, if you use Windows be cautioned about SONY music CDs. They contain 'viewer' type software that is actually a trojan horse for a "rootkit". The...
5
by: SaherAlim | last post by:
Hi, How can I record line-in and playback music simutaniously. Basically I want to merge two sounds. One comming from mic and the other is the played music from PC. i can access line-in (mic)...
4
by: Doug van Vianen | last post by:
Hi, I am using Visual Basic 6 to generate web pages that can be used by the members of our seniors' computer club to create e-cards that include their own pictures. I wish to include background...
2
by: tony.held | last post by:
Hi all, I would like to develop software that displays guitar sheet music and play it via midi. I plan on using visual studio .net 2005 (probably VB but maybe C#). I was thinking about having...
2
by: Omar | last post by:
I've been learning some programming during my little sabbatical (mostly python), and I thought it'd be cool to see if other people are interested in programming for music theory. So I started a...
0
by: Gene | last post by:
Hi, I'm a college student taking CS class, and for a project, I want to import some music or sound files into a program I wrote in C. I've read some stuff about using sndPlaySound or PlaySound but...
2
pbmods
by: pbmods | last post by:
At the request from MacMan247, I am hereby posting the 'how I did it' to get the music from ShadowWraith. Here's what I did. Turns out that the music is stored in MOD format inside the...
0
by: D'Arcy J.M. Cain | last post by:
On Sun, 28 Sep 2008 16:37:11 +0200 "Mr.SpOOn" <mr.spoon21@gmail.comwrote: Sounds interesting. Will this be Open Source? Have you considered having the object take a key option with default...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.