How can I put a full length song (3-4 Minutes) in a game. Please list the code needed and the headers that I need to add. Thank You Very Much!
Printable View
How can I put a full length song (3-4 Minutes) in a game. Please list the code needed and the headers that I need to add. Thank You Very Much!
Eh?
There is [I presume] no difference between playing a 2 second, 20 second or 3-4 minute .WAV file.
Perhaps this article will explain what you need to do:
http://www.microsoft.com/msj/0698/sound/sound.aspx
This assumes you are using Windows. If you are using Linux/Unix to write your game, I can't help, and you'll need to explain this to us.
--
Mats
Thanks for the response, but I was looking for something more like the playsound function.
Thanks
And why do you think you can't use the PlaySound function in the first place - the above was just what I found when searching for "Play DirectSound" - if you are not using DirectSound bur prefer to use the multimedia functions, then I don't see a problem with that.
So, what is it you are after, a full example version of PlaySound?
--
Mats
Thanks, I have looked on the internet and I have been unable to figure out how to use playsound. I am looking for a example on how to use it. Im very confused on how to use playsound.
Thanks
So, why don't you post a small piece of the code you are working on, with suitable questions on the bits you don't understand/can't get to work.
It looks to me like the PlaySound is really simple to use - something like:
I haven't tried that, and of course, the "name" has to be a valid wave-file of some sort.Code:#include <Windows.h>
int main()
{
TCHAR name = "c://blah.wav";
BOOL f;
f = PlaySound(name, NULL, SND_FILENAME);
if (!f) {
printf("Error\n");
}
return 0;
}
--
Mats
Here is the CodeThat was the best I could do (and it wont complile)Code:#include <iostream>
#include <string>
#include <windows>
#include <cstdlib>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
using namespace std;
int main()
{
cout << "Hello world" << endl;
system ("PAUSE");
PlaySoundA("fanfare.wav", 0, SND_RESOURCE | SND_NODEFAULT);
return 0;
return 0;
}
Thanks
What compiler errors do you get?
--
Mats
I updated to Visual 2008 and no longer get compiler erros. Unfortunately I am unable to hear the sound file. Please help. Thanks
Are you sure that the red code is correct?Code:PlaySoundA("fanfare.wav", 0, SND_RESOURCE | SND_NODEFAULT);
--
Mats
no. Do you know what the right code is? Thanks.
I thought "SND_FILENAME" would make more sense from the parameters you use - but I'm not sure at all.
I don't think "fanfare.wav" is a resource, but rather a filename.Quote:
Originally Posted by MSDN
--
Mats
Please explain by posting code. I do not understand. Thanks Very Much
For PlaySound(), there is a limit to the WAV file size. When I once tried to play a full song with PlaySound() I got an error.Quote:
How can I put a full length song (3-4 Minutes) in a game.
MSDN says:I assume "available physical memory" depends on how much RAM you have, how much RAM is being used by Windows and other applications.... And maybe how much physical RAM Windows is willing to allocate to your program....Quote:
Remarks
The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver....
codeproject.com has a DirectSound example that can play a whole song.
You mean, my example that I posted earlier isn't good enough? It does exactly what I said you should change, using SND_FILENAME as the documentation says. I'm sorry, I'm not going to write the same thing again. Of course, you WILL have to change [or copy a file to match] blah.wav. But I think you are capable of that.
But since I wanted to make sure I wasn't telling porkies, I produced this, that runs on my machine:
Of course, I missed the note about the wav file fitting in memory, so if you want to play 4 minutes of 40 KHz samples, you are looking at needing 40MB or thereabouts. (40 KHz * 2 channels * 2 bytes per sampel * 240 seconds)Code:#include <windows.h>
#include <stdio.h>
int main(int argc, char **argv)
{
BOOL f;
f = PlaySound("c:\\winnt\\media\\tada.wav", 0, SND_FILENAME);
if (!f)
printf("Error\n");
else
printf("Done!\n");
return 0;
}
--
Mats
Your post earlier generates errors. Thanks
Here is the code that I used (the code stated by you in an earlier post)
Here is the compile log:Code:#include <windows.h>
#include <cstdio>
int main(int argc, char **argv)
{
BOOL f;
f = PlaySound("c:\\Fanfare.wav", 0, SND_FILENAME);
if (!f)
printf("Error\n");
else
printf("Done!\n");
return 0;
}
------ Build started: Project: soundtest3, Configuration: Debug Win32 ------
Compiling...
soundtest3.cpp
c:\documents and settings\mom\my documents\visual studio 2008\projects\soundtest3\soundtest3\soundtest3.cpp (7) : error C2664: 'PlaySoundW' : cannot convert parameter 1 from 'const char [15]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Documents and Settings\Mom\My Documents\Visual Studio 2008\Projects\soundtest3\soundtest3\Debug\BuildLog .htm"
soundtest3 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have the Fanfare.wav file on my C drive in not specific folder. I attempted to use PlaysoundA and still received errors.
Thanks Very Much
Well, I've already posted a working version. However, if you compile with Unicode enabled [as it seems you do], then you need to use the appropriate "make unicode out of text-string" such as TEXT("some text") or _T("some text") [those are both doing the same thing, and I can't remember which you use for which type of application - try one, and if it doesn't work, use the other.
The latter code I posted works on my machine without errors and warnings (using Visual Studio .Net), but it's entirely possible to set the compile flags or otherwise change settings. You also need to set the right library to be linked in, either using the pragma setting that you have in your example, or using the "additional libraries" in the linker tab on Visual Studios project properties page.
--
Mats