I really want to add sounds to my programs, but all of the codes I use fail!

I am using an empty project in visual c++, if anyone could find a snippet that would work for this, I would be extremely grateful. Thanks!

Here is my current code that doesn't work:

// use mciSendString() to play and stop a midi music file
// you have to link with the winmm.lib file, or
// in the case of Dev-C++ link with libwinmm.a via
// Project>>Project Options>>Parameters>>Add Lib>>libwinmm.a
// a Dev-C++  console application
 
#include <iostream>
#include <windows.h>
#include <mmsystem.h>  // mciSendString()
#include <conio.h>
#include <string>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;   // std::cout, std::cin
 
void Play()
{
wstring file;
wstring Command;

file = L"H:\\sissys.mp3";

Command = L"play " + file + L" from 0";

mciSendString(Command.c_str(), NULL, 0, 0);
}

Perhaps the PlaySound function?

PlaySound("H:\\sissys.mp3", NULL, SND_FILENAME);

Sadly that doesn't work for some reason..

You'll also need to include "vfw.h" and Vfw32.lib must be added to project link input Object/Library modules

CString m_Path = locAUD; // path to sound
HWND m_Video = NULL;
if(m_Video == NULL)
	m_Video = MCIWndCreate(this->GetSafeHwnd(),	AfxGetInstanceHandle(), WS_CHILD | WS_VISIBLE|MCIWNDF_NOMENU,m_Path);
else
	MCIWndHome(m_Video);
MCIWndPlay(m_Video);

Hmm, it still doesn't work. >.<

Could you please just give me a full example with headers and such? I am really new at this, and have been searching how to play sound for 17 days.

Hmm, it still doesn't work. >.<

Could you please just give me a full example with headers and such? I am really new at this, and have been searching how to play sound for 17 days.

That's pretty much all there is to it. That code is in a function that I call from elsewhere...the rest of the code is several tens of thousands of lines of unrelated code.

What is it doing that it doesn't work? Not compiling, not playing, etc.? Did you #include "vfw.h" and add the Vfw32.lib business to the project link input Object/Library modules? My Visual Studio here at home is messed up, so I can't recall exactly where to add that, but it's under project | options or something similar on the toolbar...the one that brings up all the project option tabs.

Hmm. I cant get it working either.

#include <cstdlib>
#include <windows.h>
#pragma comment(lib, "Winmm")
#pragma comment(lib, "Vfw32")
#include <vfw.h>
#include <Mmsystem.h>

using namespace std;

LPCWSTR lpFileName = L"H:\\sissys.mp3";

int wmain(int argc, char **argv)
{
PlaySound(lpFileName, NULL, SND_FILENAME);
return 0;
}
commented: Thanks for the help +1

Hmm. I cant get it working either.

#include <cstdlib>
#include <windows.h>
#pragma comment(lib, "Winmm")
#pragma comment(lib, "Vfw32")
#include <vfw.h>
#include <Mmsystem.h>

using namespace std;

LPCWSTR lpFileName = L"H:\\sissys.mp3";

int wmain(int argc, char **argv)
{
PlaySound(lpFileName, NULL, SND_FILENAME);
return 0;
}

That worked perfectly, thanks!

Oh, awesome. Wouldnt work for me. maybe because i was using the express edition (doesnt come with some libries)?

Nope, express works fine. Just get rid of the "H://" before the file name, and make sure that the music you are referencing to is in the same folder as the .cpp file being used to play it.

Hope you get it working! Also, if you don't have a sound to play, use this site: http://www.flashkit.com/loops/Easy_Listening/Classical/

It has some epic loops! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.