I've got a Visual C++/CLI app which uses beeps to signify good and bad results (used when the user can't see the screen).
Currently I use low pitched beeps for bad results and high pitched beeps for good results:
if( goodResu开发者_开发百科lt == true )
{
Beep(1000, 40);
}
else
{
Beep(2000, 20);
}
This works okay on my Vista laptop, but I've tried it on other laptops and some seem to play the sounds for less time (they sound more like clicks than beeps) or the sound doesn't play at all.
So I have two questions here:
- Is there a more reliable beep function?
- Is there a (simple) way I can play a short .wav file or something similar instead (preferred solution).
The Beep function traditionally used the PC speaker, and I guess it is more or less obsolete nowadays. I guess the functionality is handled by the Laptop's BIOS, and thus is hardware dependant. I suggest that you use the PC:s soundcard to play the sound instead.
Create two tones as WAV files in e.g. Audacity, then play them using e.g. PlaySound.
Based on Krumelur's answer, I got a fairly neat solution which embeds the .wav's in the executable. It uses SoundPlayer
instead of the PlaySound
function.
For a Visual C++ / CLI project, adding embedded resources seems to work slightly differently to the other languages .net supports:
- Right-click on the project and select
Properties
. - Navigate to
Configuration Properties -> Linker -> Input
. - Add the paths to the .wav file(s) you want to embed (in my case,
$(ProjectDir)\Audio\PingSend.wav
).
To play back one of the .wav's simply call:
System::IO::Stream^ s = Assembly::GetExecutingAssembly()->GetManifestResourceStream("PingSend.wav");
System::Media::SoundPlayer^ pingPlayer = gcnew System::Media::SoundPlayer(s);
pingPlayer->Play();
..which should play the sound in a new thread.
Perhaps speech synthesis might be appropriate, not sure, I am assuming your users are blind.
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.aspx
The Windows MessageBeep function will play the default error, OK or alert system sounds with the correct parameters. As such, the actual sound played and volume is under the user's control, but should be recognisable as error or OK.
It looks like there might be a solution using the winmm library.
精彩评论