9 Replies - 10160 Views - Last Post: 21 July 2011 - 02:09 PM Rate Topic: -----

#1 vader7071  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 11

Play Sound file in C++

Posted 21 July 2011 - 11:51 AM

I am using MS Visual Studio 2010 as my compiler (windows 7).

I am making a console app in C++.

I am working on trying to get my solution to play a sound file at the beginning (1 time shot, for only about 2 seconds).

I have searched and found the code command
PlaySound("C:\\playgame.wav", NULL, SND_ASYNC);


I have tried loading this code (and many variations such as PlaySoundA, PlaySoundW, PlaySound(TEXT("C:\\.....), NULL...)

I get an error message of error C2664: 'PlaySoundW' : cannot convert parameter 1 from 'const char [16]' to 'LPCWSTR'

Any suggestions on how to correct this issue?

Is This A Good Question/Topic? 0
  • +

Replies To: Play Sound file in C++

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Play Sound file in C++

Posted 21 July 2011 - 12:58 PM

I never really had much luck with PlaySound either. Here is an example I did (back in this post) based upon info on MSDN:
#include <iostream>
#include <windows.h>
#pragma comment (lib , "winmm.lib")
using namespace std;

int main()
{
	UINT wDeviceID;  //a device ID so we can keep track of it once we open it.
	DWORD dwReturn;	//a return value..
	MCI_OPEN_PARMS mciOpenParms; //Structure for the MCI_OPEN command
	MCI_PLAY_PARMS mciPlayParms; //Structure for the MCI_PLAY command
 
	// Opens a waveform-audio device by specifying the device and 
	// file name.

	mciOpenParms.lpstrDeviceType = "waveaudio";
	mciOpenParms.lpstrElementName = "C:\\Users\\NickDMax\\Pictures\\web stuffs\\chickens.wav";

	//Not the constant MCI_OPEN_TYPE & MCI_OPEN_ELEMENT tell the function which fields of mciOpenParms actually
	//   have something usefull in them.
	if (dwReturn = mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)(LPVOID) &mciOpenParms))
	{
	
		cout << "unable to open device.\n";
	}

	// The device opened successfully; get the device ID.
	wDeviceID = mciOpenParms.wDeviceID;
	
	// The lines that are commented out would be used to set up a notification for when the
	// wave file is done, however since we are in the console I removed this bit and went with
	// a Sleep() to pause until the playback should be over.
	
	//mciPlayParms.dwCallback=(DWORD) hWndNotify;
	//mciPlayParms.dwFrom=0;
	//mciPlayParms.dwTo=0;

	//if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)(LPVOID) &mciPlayParms))
	if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, 0, NULL))
	{
		mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
		cout << "no go\n";
		return (dwReturn);
	}

	Sleep(16000); //Length of the wav file... you just need to make sure your program has enough time
	//  to play the file before it closes the device. Else it will cut it short, or not play at all.
	mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
	return 0;
}


it works for me on VS2010 win 7 -- but I was not using unicode...
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Play Sound file in C++

Posted 21 July 2011 - 12:59 PM

PlaySound(TEXT("playgame.wav"), NULL, SND_FILENAME);

try that. dose that work?

if you pass a file name you have to make sure the SND_FILENAME option is set. to make it ASYNC you can use the or operator

PlaySound(TEXT("playgame.wav"), NULL, SND_FILENAME | SND_ASYNC);

This post has been edited by ishkabible: 21 July 2011 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 vader7071  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 11

Re: Play Sound file in C++

Posted 21 July 2011 - 01:21 PM

@ishkabible, it did not work. Same error code.

@NickDMax, I tried using your code, and changed the file location, and I got errors on line 16 and 17.

error C2440: '=' : cannot convert from 'const char [10]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



(line 17 has char [16] in place of char [10]).

Thanks guys for all of your help.
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Play Sound file in C++

Posted 21 July 2011 - 01:27 PM

use TEXT, it converts c strings to wide strings.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Play Sound file in C++

Posted 21 July 2011 - 01:34 PM

View Postvader7071, on 21 July 2011 - 04:21 PM, said:

@ishkabible, it did not work. Same error code.

@NickDMax, I tried using your code, and changed the file location, and I got errors on line 16 and 17.

error C2440: '=' : cannot convert from 'const char [10]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



(line 17 has char [16] in place of char [10]).

Thanks guys for all of your help.


yes -- you are using unicode. so change those two lines to:

	mciOpenParms.lpstrDeviceType = L"waveaudio";
	mciOpenParms.lpstrElementName = L"C:\\Users\\Nicholas\\Pictures\\web stuffs\\chickens.wav";


OR
	mciOpenParms.lpstrDeviceType = TEXT("waveaudio");
	mciOpenParms.lpstrElementName = TEXT("C:\\Users\\Nicholas\\Pictures\\web stuffs\\chickens.wav");

Was This Post Helpful? 0
  • +
  • -

#7 vader7071  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 11

Re: Play Sound file in C++

Posted 21 July 2011 - 01:48 PM

That worked PERFECTLY!

I am currently in a C++ programming class, and myself and a couple of other students have taken it upon ourselves to mess with the instructor.

Our first bit was doing a "Tron" login sequence at the beginning of everyone of our programs, and now we have decided to toy with him more by adding the "Shall we play a game" audio file from "WarGames".

I got the text typing smoothly with the audio file now, and everything seems to be working great.

But I do have one more quick question. How can I make the audio file location dynamic instead of absolute?

Right now it is set to C:\(file).wav I would like it to look in the same folder as the program is running from. Can I just remove the "C:\\" from the file location and it look in the solution directory?
Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Play Sound file in C++

Posted 21 July 2011 - 01:58 PM

yes
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Play Sound file in C++

Posted 21 July 2011 - 02:03 PM

If there is no path (just a file name) then windows will assume the "current directory" which *usually* is the directory that the .exe is in. So when you do this in VS for example you need to make sure that the wav file gets placed into the correct debug/release directory (I have no idea how, I would just copy it over but I am sure there is some way to add it to the project or something).

You have to keep in mind that users may get tripped up by this if they run the program from the command prompt (which is an easy way to have the "current directory" not match the executable's directory).
Was This Post Helpful? 0
  • +
  • -

#10 vader7071  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-June 11

Re: Play Sound file in C++

Posted 21 July 2011 - 02:09 PM

I moved the file within the same directory as the *.cpp file I am using and that worked. We are not compiling into a full *.exe file yet. We load the solution in VS2010 and just run debug mode for now, so being in the same folder as the *.cpp file seems to be working.

Thanks again guys (Nick and Ishkabible) for your help. This is perfect for what I need.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1