MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / SoundPlay

Method SoundPlay

source/script2.cpp:7572–7624  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7570
7571
7572ResultType Line::SoundPlay(LPTSTR aFilespec, bool aSleepUntilDone)
7573{
7574 LPTSTR cp = omit_leading_whitespace(aFilespec);
7575 if (*cp == '*')
7576 {
7577 // ATOU() returns 0xFFFFFFFF for -1, which is relied upon to support the -1 sound.
7578 // Even if there are no enabled sound devices, MessageBeep() indicates success
7579 // (tested on Windows 10). It's hard to imagine how the script would handle a
7580 // failure anyway, so just omit error checking.
7581 MessageBeep(ATOU(cp + 1));
7582 return OK;
7583 }
7584 // See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_play.asp
7585 // for some documentation mciSendString() and related.
7586 // MAX_PATH note: There's no chance this API supports long paths even on Windows 10.
7587 // Research indicates it limits paths to 127 chars (not even MAX_PATH), but since there's
7588 // no apparent benefit to reducing it, we'll keep this size to ensure backward-compatibility.
7589 // Note that using a relative path does not help, but using short (8.3) names does.
7590 TCHAR buf[MAX_PATH * 2]; // Allow room for filename and commands.
7591 mciSendString(_T("status ") SOUNDPLAY_ALIAS _T(" mode"), buf, _countof(buf), NULL);
7592 if (*buf) // "playing" or "stopped" (so close it before trying to re-open with a new aFilespec).
7593 mciSendString(_T("close ") SOUNDPLAY_ALIAS, NULL, 0, NULL);
7594 sntprintf(buf, _countof(buf), _T("open \"%s\" alias ") SOUNDPLAY_ALIAS, aFilespec);
7595 if (mciSendString(buf, NULL, 0, NULL)) // Failure.
7596 goto error;
7597 g_SoundWasPlayed = true; // For use by Script's destructor.
7598 if (mciSendString(_T("play ") SOUNDPLAY_ALIAS, NULL, 0, NULL)) // Failure.
7599 goto error;
7600 // Otherwise, the sound is now playing.
7601 if (!aSleepUntilDone)
7602 return OK;
7603 // Otherwise, caller wants us to wait until the file is done playing. To allow our app to remain
7604 // responsive during this time, use a loop that checks our message queue:
7605 // Older method: "mciSendString("play " SOUNDPLAY_ALIAS " wait", NULL, 0, NULL)"
7606 for (;;)
7607 {
7608 mciSendString(_T("status ") SOUNDPLAY_ALIAS _T(" mode"), buf, _countof(buf), NULL);
7609 if (!*buf) // Probably can't happen given the state we're in.
7610 break;
7611 if (!_tcscmp(buf, _T("stopped"))) // The sound is done playing.
7612 {
7613 mciSendString(_T("close ") SOUNDPLAY_ALIAS, NULL, 0, NULL);
7614 break;
7615 }
7616 // Sleep a little longer than normal because I'm not sure how much overhead
7617 // and CPU utilization the above incurs:
7618 MsgSleep(20);
7619 }
7620 return OK;
7621
7622error:
7623 return LineError(ERR_FAILED, FAIL_OR_OK);
7624}
7625
7626
7627

Callers

nothing calls this directly

Calls 3

omit_leading_whitespaceFunction · 0.85
sntprintfFunction · 0.85
MsgSleepFunction · 0.85

Tested by

no test coverage detected