Thread: play sound in C?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    71

    play sound in C?

    hello! it is possible to play sound in C ?
    outside the function Beep ()?

    I want to play a "*.wav" or "*.mp3" , if that is possible


    Le.:I use windows xp sp2 + codeblocks with mingw
    Last edited by nutzu2010; 01-01-2011 at 03:30 AM.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Yes. Ask Google how.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
        PlaySound("c:\\bird.wav", NULL, SND_ASYNC);
        return 0;
    
    }

    This error occurs:

    Code:
    Compiling: C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\Untitled2.c
    Linking console executable: C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\Untitled2.exe
    C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\Untitled2.o:Untitled2.c:(.text+0x42): undefined reference to `_PlaySoundA@12'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    1 errors, 0 warnings

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Close. Although you have included the appropriate header file, the matching library is not being linked in automagically, as is the case with standard headers.

    You said that you're using MinGW. In that case, all you need to do is add "-lwinmm" to your compile options.

    If you were compiling purely from CLI, it would look something like this:
    Code:
    gcc play_sound.c -lwinmm
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    sorry to ask such a silly, but how do I add "-lwinmm" in compiler options?
    settings->compiler and debugger settings ,and now?

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I have no experience with Code::Blocks. I'd hazard a guess and suggest that you look under Project/Build or equivalent. Maybe someone who has worked with C::B will come along and give you a hint.

    Alternatively, you could compile from CLI.
    Code:
    Windows > Run > "cmd"
    cd /d "C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\"
    gcc Untitled2.c -oplay_sound -lwinmm
    play_sound.exe
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    Quote Originally Posted by msh View Post

    Alternatively, you could compile from CLI.
    Code:
    Windows > Run > "cmd"
    cd /d "C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\"
    gcc Untitled2.c -oplay_sound -lwinmm
    play_sound.exe
    'gcc' is not recognized as an internal or external command,operable program or batch file.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Linux reply

    does anybody know how to play a sound using linux instead of windows.

    thanks

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, you google "linux sound" or "linux audio". I would have thought that obvious enough to have tried before posting (or hijacking someone else's thread).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by msh View Post
    I have no experience with Code::Blocks. I'd hazard a guess and suggest that you look under Project/Build or equivalent. Maybe someone who has worked with C::B will come along and give you a hint.

    Alternatively, you could compile from CLI.
    Code:
    Windows > Run > "cmd"
    cd /d "C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\"
    gcc Untitled2.c -oplay_sound -lwinmm
    play_sound.exe
    If you got to compiler options, choose "Linker options" and then add a library where it says add library.

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    i have add to "Linker options":
    Code:
     
    gcc play_sound.c -lwinmm
    and i have the error:
    Code:
    Compiling: C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\Untitled2.c
    Linking console executable: C:\Documents and Settings\i'm vochin\My Documents\code blocks project's\teste\Untitled2.exe
    C:\Program Files\CodeBlocks\MinGW\bin\ld.exe: cannot find -lgcc play_sound.c -lwinmm
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    1 errors, 0 warnings

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, "gcc play_sound.c -lwinmm" is a long way off from "winmm" I should think.

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    I wanted to put "-lwinmm"

    thanks!worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX Question
    By bladerunner627 in forum Game Programming
    Replies: 2
    Last Post: 04-04-2005, 11:55 AM
  2. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Gui Class With Tic Tac Toe
    By xxYukoxx in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2003, 04:28 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM