Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can i play a sound (CPU Beep or wav, don't matter what) using the Windows cmd?

share|improve this question

migrated from stackoverflow.com Jan 29 '10 at 2:01

This question came from our site for professional and enthusiast programmers.

2  
Using start file.wav is a bad idea. It might take a second to start a bloated media player, just for a single beep. In addition, file associations might be wrong, the media player might not play the file, or it might play it over and over again, etc. The way of creating a simple "beep" is to write beep ^G. "^G" is not the circumflex accent followed by a capital letter G, but rather a special character that you insert by pressing Ctrl+G. It is actually the BEL character with ASCII value 0x07. –  Andreas Rejbrand Aug 25 '10 at 20:30

9 Answers 9

up vote 3 down vote accepted

Not in windows now, in order to test this possible solution, but try to: "start "

I think it will open the wav file with the associated program that your windows has for ".wav" files.

And note, this is a wild-guess - someone with windows may give you a better solution if this doesn't do the job

share|improve this answer
    
Well, it'll certainly open the file in whatever application is associated with the type, but that's it. It's up to that application to decide what it does with it - it might play it, it might add it to a queue, or something else. Even if it does play the file, it's entirely likely that it won't terminate after playing, meaning this is probably a bad idea. –  Michael Madsen Jan 29 '10 at 0:31
    
That's all what I needed for now.. Thanks :) –  TiuTalk Jan 29 '10 at 0:53

You can do this natively with PowerShell. PowerShell is included with Windows Vista and later, and can be downloaded from Microsoft for older versions.


Wave files

PowerShell can be used to load the System.Media.SoundPlayer .NET class, which can be used to play a wave file.

(New-Object Media.SoundPlayer "C:\WINDOWS\Media\notify.wav").Play();

If you want, you can run this from the normal command line:

powershell -c (New-Object Media.SoundPlayer "C:\Windows\Media\notify.wav").PlaySync();

(note that PlaySync is used in the second example since the standard asynchronous play would be interrupted by the PowerShell process closing when launched like this)

And if you wanted to play only the first, say, 5 seconds of the sound:

powershell -c (New-Object Media.SoundPlayer "C:\Windows\Media\notify.wav").Play(); Start-Sleep -s 5; Exit;

Beep

A beep can be easily accomplished in the normal command line with echo ^G (where ^G represents BEL, ASCII character 7, inserted with Ctrl + G), as described in other answers. In the interest of completeness, here's the PowerShell method:

echo ^G

Yes, it's the same as the cmd one. echo in PowerShell is an alias (i.e. means the same thing) to Write-Host, which displays something to the screen (or triggers the Windows notification sound in the case of BEL).

An alternative method in PowerShell is to use the escape sequence for BEL, rather than inserting a literal BEL character with Ctrl + G:

echo `a

` is PowerShell's escape character, which modifies the meaning of the character after it. An escaped a indicates BEL. The advantage of this approach is it is easier and more visible when typed into a script.

To run this in a batch file (again, Vista or later):

powershell -c echo `a

source

share|improve this answer
    
Powershell, good idea :) –  Josh Comley Feb 3 '13 at 23:37
    
This should be the top answer as it's the most complete and satisfied the stackex mission the best. Please upvote this. –  Nathan C. Tresch Nov 25 '13 at 22:57
    
Is it possible to also have this command kill and currently playing wav files before executing the new wav file? –  Dodd10x Jul 21 at 15:39
    
@Dodd10x Not easily for global audio, no. Could you provide more context? Do you intend to kill or pause? Is the other player another instance of this script, or a media player, or a browser, a game, etc.? If it's another instance of the same script, then it's much simpler (though still not entirely trivial). –  Bob Jul 21 at 15:46
    
It's another instance of the same script. I'm thinking I could add a line to kill powershell.exe in the task list first –  Dodd10x Jul 21 at 15:49
echo ^G

Where ^G is CTRL + G or Alt + 7 on the keypad.

share|improve this answer

If a plain beep is alright, echo the character with the value 7, the so-called bell character. Note, however, that beeps can be turned off.

If you want something else, you'll have to launch an application that does the trick.

share|improve this answer

You could write a simple console application that took the sound file (or sound id) as an argument and called PlaySound

share|improve this answer

Download this , save it somewhere. In a cmd window run:

type beep

The file holds just a single character - the BEL character . Ascii value 7.

share|improve this answer

Install VLC. Use the following command. It starts up REALLY fast. This is what I used on Windows 7 b/c wmplayer takes so long to load, and the /close option was removed from wmplayer.

vlc.exe --play-and-exit audio.wav
share|improve this answer

On XP I do this

start /min mplay32 /play /close %WINDIR%\media\tada.wav

It's not ideal but it's really easy and it works.

share|improve this answer

I use mplayer for this. A bit an overkill as it can play almost any media file. Recent windows builds can be found at spirton, as of 2013. Example usage:

mplayer c:\windows\media\chimes.wav

You should add mplayer.exe to your PATH (see What are PATH and other environment variables, and how can I set or use them? or How can I permanently append an entry into the system's PATH variable, via command line? to do this.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.