There is a problem with adding sound to my application...

This is a discussion on There is a problem with adding sound to my application... within the C++ Programming forums, part of the General Programming Boards category; Im making a console app, but the idea is that there will be music in the background, but I can't ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question There is a problem with adding sound to my application...

    Im making a console app, but the idea is that there will be music in the background, but I can't get it to play, all I get are linker errors when I try to compile. Ive included the "mmsystem.h" file, and am using the PlaySound(); function, but it keeps on coming up with this error

    Code:
    Consolechess.cpp: undefined reference to `PlaySoundA@12'

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    You may need to include the Winmm library also:
    Code:
    // Library
    #pragma comment(lib, "winmm.lib")
    
    // Headers
    #include <windows.h>
    #include <mmsystem.h>
    There are other ways to link libraries. Though, for reference, winmm.lib, is the library needed.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Close but no cigar, sorry, but the same thing happened, try again though, Im open to suggestions.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,651
    Stack Overflow is correct.

    "#pragma comment(lib, "winmm.lib")" is an MSVC specific pragma for linking a library. If you are not using MSVC, then you'll have to link with the WinMM library using compiler or linker command line options.

    If you are using Dev-C++ as your sig indicates, then simply add "-lwinmm" to the linker's parameters

    gg

  5. #5
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Do you have any idea on how to set it as a command line parameter in Dev-C++, because Ive never had the need to do it before?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,651
    Using Dev-C++ 4.9.8.0:
    Project menu -> Project Options -> Parameters tab -> add "-lwinmm" in edit box under Linker.

    gg

  7. #7
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    for some reason when I do it, it plays a different sound, it plays the same sound as if you compiled the code below (I can't think of a better way of explaining it), just try this code and listen for the sound, thats the sound I mean. It doesn't play the sound I want
    Code:
    MessageBox(NULL, "TEST", "TEST", MB_OK);

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,651
    Show us all the relevant code.

    gg

  9. #9
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Code:
    #include <mmsystem.h>
    I added the relavent .lib file
    Code:
    PlaySound("background.midi", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,651
    PlaySound() only supports waveform-audio files, which MIDI files are not.

    Here are a couple of resources for playing MIDI files:
    http://www.codeguru.com/Cpp/G-M/mult...cle.php/c4715/
    http://msdn.microsoft.com/library/de...vcsmpaudio.asp

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with adding up numbers from a txt file
    By Leo12 in forum C Programming
    Replies: 1
    Last Post: 02-18-2009, 12:50 PM
  2. What's problem of adding two binary number?
    By Mathsniper in forum C Programming
    Replies: 1
    Last Post: 01-12-2007, 05:12 AM
  3. Problem w/ console application compadibility
    By DarkMortar in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 07:13 PM
  4. C# VS2005 - Problem publishing application
    By jcrcarmo in forum C# Programming
    Replies: 2
    Last Post: 03-01-2006, 05:47 AM
  5. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 12:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21