-1

I have created the game snake in C that will run on Windows. I want to add sounds when the user hits certain keys or if the snake does certain things.

I know how to do this in C on MacOS but I don't know how to do it on Windows.

I tried to compile using a playsound() function I found on Google but I get an error message that indicates that playsound doesn't exist.

I tried to play the sound played by Windows when you type control G into run but this led me down a horrible rabbit hole of never ending and not helpful code.

I have no code to post at this time.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Jennifer
  • 19
  • 1
  • 1
  • 1
    Windows [playsound function](https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx) does exist. An example is shown on the man page. Note too it says *"The SND_ASYNC flag causes PlaySound to return immediately without waiting for the sound to finish playing."*. You will need to `#include ` and link the library mentioned too. – Weather Vane Nov 16 '16 at 17:59
  • You must have some code to share if you've *tried to use the playsound function*. Have you included the winmm library in your compiler settings - and included it in your code? – dbmitch Nov 16 '16 at 18:01
  • http://stackoverflow.com/a/1565518/2419128 – dbmitch Nov 16 '16 at 18:02
  • 2
    Welcome to Stack Overflow. Please check out [the tour](http://stackoverflow.com/tour), and thoroughly read [the help pages](http://stackoverflow.com/help) - in particular, ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic), ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask), ["How do I ask a good question?"](http://stackoverflow.com/help/how-to-ask) and ["How to create a Minimal, Complete, and Verifiable example"](http://stackoverflow.com/help/mcve). As-is, your question is off-topic for this site. – Random Davis Nov 16 '16 at 18:09
  • 1
    The function `playsound()` which you use does not exist, the one I linked is named `PlaySound()`. – Weather Vane Nov 16 '16 at 18:11
  • Weather Vane: So I found out that my problem was that I completely ignored the fact that I need the winmm library. The function does exist. I am just a novice :) – Jennifer Nov 17 '16 at 20:01
  • Plus, I also had playsound() ranther than PlaySound(). And thank you for reminding me of the winmm library, dbmitch. Finally, I am sorry if this question was off-topic, etc. I have no idea what I am doing. Clearly. – Jennifer Nov 17 '16 at 20:04

3 Answers3

0

I would suggest looking into the SDL which is a very reliable, widespread and cross-platform library. It has many features providing low-level control over the IO devices (keyboard, controller, mouse, graphics via OpenGL, sound...).

A. Grenouilloux
  • 69
  • 1
  • 1
  • 5
0

I encountered a similar issue with a game I'm working on - if by chance you are using the Win32 API (windows.h), you should be able to leverage the PlaySound function, however it can only ever play (mix) one sound at a time (e.g. no music and sound effects). Note that as Weather Vane pointed out in his comment, capitalization matters (PlaySound vs playsound). If you only need one sound and you're using the Win32 API, figuring out the PlaySound() function would be the easiest. Here's a SO question that has a basic example which should work in C.

I ended up using OpenAL (analogous to OpenGL) which allows you to play several sounds at once. I'd suggest searching for some simple, complete examples.

Community
  • 1
  • 1
schil227
  • 292
  • 3
  • 11
  • Thank you! I did not know that PlaySound() can only play one sound at a time. – Jennifer Nov 17 '16 at 20:05
  • Nothin' says thanks like an upvote :) As a side comment, if you do end up going the OpenAL route, know that setting it up is a major pain in the ass, but once it works it'll fulfill all your audio needs. – schil227 Nov 17 '16 at 20:13
0

I just like getting into need, Follow this: Include this header file:

mmsystem.h

In the main type this: PlaySound(TEXT("NameOfAudio.wav"),NULL,SND_LOOP);

Put the wav file you want to play in the folder that contains the project file and you are good to go!