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);
}

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);

Edited 7 Years Ago by donaldw: n/a

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;
}
Comments
Thanks for the help

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)?

This question has already been answered. Start a new discussion instead.