CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2009
    Posts
    23

    How do I play an audio file?

    (Please note that I extended the question in post #5!)

    Hello,

    I am new to the board and new to C#. I installed MS Visual C# '08 EE and got started with some ideas I had. Everything went way better than I expected and I already managed to realise some things that I thought would take me ages.

    I have an idea for a rather simple programme. I wrote all the basic stuff already but one of the crucial parts of the programme is that it has to play sounds.

    That is, I'll have a folder with several sounds files (mp3's) that should be played when I press a corresponding button.

    As I said, I am using Visual C# and have created a Form already. That Form has several buttons which I now want to link to some kind of "play sound file X" function. And, to be honest, I have no idea how to do that.

    I used google to find an answer but couldn't find anything that made sense to me. Could someone point me in the right direction and give a newbie-friendly explanation?

    All help is gratefully received!

    Thank you very much in advance and have a nice day!
    Last edited by TryingToC#; December 16th, 2009 at 07:42 PM.

  2. #2
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    Re: How do I play an audio file?

    Spent like, 30 seconds with google and I found this:

    http://www.daniweb.com/code/snippet217151.html

    Enjoy

  3. #3
    Join Date
    Dec 2009
    Posts
    23

    Re: How do I play an audio file?

    Oh, I am sorry about that. I'll try to implement that and come back here if I fail, hehe. Thanks for the hint and sorry for the useless post!

  4. #4
    Join Date
    Dec 2009
    Posts
    23

    Re: How do I play an audio file?

    @ ixilom: Just wanted to say "Thank you" - it works!

  5. #5
    Join Date
    Dec 2009
    Posts
    23

    Re: How do I play an audio file?

    (I didn't want to create another thread for this so I continue here...)

    I implemented the method that was suggested on the page ixilom linked to. This is what I did:

    Code:
    public void PlayWAV(String Location)
    {
            if (System.IO.File.Exists(Location))
            {
                System.Media.SoundPlayer Sound = new System.Media.SoundPlayer(Location);
                Sound.Play();
            }
            else
                MessageBox.Show("File does not exist.", "Error");
    }
    This works just fine. My follow-up question is: How can I adjust the volume of the played sound?

    Thanks for reading!

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How do I play an audio file?

    Quote Originally Posted by TryingToC# View Post
    This works just fine. My follow-up question is: How can I adjust the volume of the played sound?
    Unless it is absolutely required, I would think that it should play at whatever the current system sound level is at. Unless you are writing a program like WMP, I would not want you mucking with my sound settings.

  7. #7
    Join Date
    Dec 2009
    Posts
    23

    Re: How do I play an audio file?

    I want to write a metronome that can emphasise every n-th note. I wanted to realise the emphasis by having every n-th note played louder than the others.

    Does your answer mean that there's no easy way of doing that?

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How do I play an audio file?

    Quote Originally Posted by TryingToC# View Post
    I want to write a metronome that can emphasise every n-th note. I wanted to realise the emphasis by having every n-th note played louder than the others.

    Does your answer mean that there's no easy way of doing that?
    No, my response just meant that, in general, this sort of thing is a bad idea. You seem to have a valid reason to do it. I have never done anything like this personally, but I found a code project article which explains how to emulate volume ranges without actually changing the system sound level.

  9. #9
    Join Date
    Dec 2009
    Posts
    23

    Re: How do I play an audio file?

    Would you mind linking to it or point me in the direction I'd have to look? (Please keep in mind that I am a beginner, hehe.) Anyways, good to know it's possible.

  10. #10
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    Re: How do I play an audio file?

    The most simplistic way would be having two .wav samples, one louder than the other

    This means of course that you need two separate SoundPlayers and decide which one to play.
    Or you could load the needed sample for every metronome "beat" (Sorry, I'm musically impaired ) to the single SoundPlayer class.
    I would go for the former solution.

    I agree with BigEd though, don't fiddle with users volume settings unless its the purpose of your program.
    If this program is for your own personal usage and you still want to do it, I suppose you could do some interop calls for winmm.dll. Have a look at http://www.dreamincode.net/forums/showtopic45693.htm
    It is a rather long article, but it does cover a lot to do with volume and the mixer.

  11. #11
    Join Date
    Dec 2009
    Posts
    23

    Re: How do I play an audio file?

    Hey ixilom!

    Yes, that's how I solved the problem for now: Two different .wav files. I simply adjust the Location I pass to the PlayWAV() function to alternate the sounds. And it works.

    It rather was a matter of interest. I wouldn't want to change the system's volume for audio output but merely the volume for the sound itself - as you'd do when fiddling with the volume slider in WMP etc.

    Thank you for the link. I am rather busy these days but I'll get back to you if I succeed or have more questions.


    Edit:
    Just had a quick look and read this:
    Before we get into any code, you need to understand that this tutorial in an advanced tutorial, using Win32 API calls, custom structures, and the like. If you are not familiar with those items, I suggest you study them before attempting this tutorial.
    I am everything but advanced.
    Last edited by TryingToC#; December 18th, 2009 at 05:04 AM.

  12. #12
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How do I play an audio file?

    Sorry about the link, I was having forum issues yesterday trouble posting and editing. Glad to see that you have something working .

  13. #13
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    Re: How do I play an audio file?

    I am everything but advanced.
    Meh, C# is rather easy to learn, you'll be advanced by their definition in no time

  14. #14
    Join Date
    Dec 2009
    Posts
    23

    Re: How do I play an audio file?

    Just as an update to this thread. I ended up using something else than discussed above. I had problems with the use of DirectX and a friend sent this to me:

    .Net 3.5 has a built-in soundplayer class. It's only able to play WAV files though:

    http://msdn.microsoft.com/en-us/library/4y171b18.aspx

    A more reliable way for MP3s is to use the media player library:

    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    Last edited by TryingToC#; January 2nd, 2010 at 04:29 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured