41

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

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
Thiago Belem
  • 689
  • 3
  • 9
  • 15
  • 4
    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

14 Answers14

76

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

Bob
  • 60,938
  • 25
  • 191
  • 216
  • Is it possible to also have this command kill and currently playing wav files before executing the new wav file? – Dodd10x Jul 21 '14 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 '14 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 '14 at 15:49
  • 1
    @Dodd10x There are two ways I would suggest. One is to get the process ID of the PowerShell instance and save it to a file, then kill that ID at the start of a new instance (and clear the file). This is safer than killing all PS processes. The other way would be to use a semaphore (flag/signal) in PowerShell - you'd signal the semaphore, use `Play()` (async), wait for a semaphore, and close. Semaphores are a cleaner way of doing this, but a bit more complicated. I'd be happy to go into further detail [in chat](http://chat.stackexchange.com/rooms/118/root-access). – Bob Jul 21 '14 at 15:53
  • This is the best answer. Bountied +50 reps. – dr_ Jul 12 '20 at 13:25
  • 1
    Please note that you have to use a backtick ` to escape spaces in file names, e.g. `powershell -c (New-Object Media.SoundPlayer "c:\Windows\Media\Windows\` Notify\` Calendar.wav").PlaySync();` – Thomas H. Schmidt Feb 22 '21 at 14:03
15
echo ^G

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

Nick Chammas
  • 105
  • 5
theMike
  • 151
  • 3
6

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
Mark
  • 169
  • 1
  • 6
  • Unfortunately this is running on foreground – honzajde Feb 07 '17 at 06:37
  • Yea, this was intended to replace the wmplayer method since it was slow to load and they removed support for the /close argument. The PowerShell method is probably the best bet. – Mark Feb 08 '17 at 03:26
  • 3
    For those looking for a headless VLC solution, `vlc -I dummy --dummy-quiet t.mp3 vlc://quit` from https://forum.videolan.org/viewtopic.php?t=79516#p262603 – MLM Dec 28 '18 at 19:20
5

Workaround (some sort of):

1) run audio file

2) wait till track ends (in my case its 5 seconds) and close media player

start wmplayer "C:\Windows\Media\Alarm10.wav" && timeout 5 && taskkill /im wmplayer.exe
yuliskov
  • 441
  • 5
  • 7
2

(While an old thread, people will find it on a search). I see someone has already recommended VLC. I use it to play a sound in Windows without anything visible using parameters: "c:\Program Files (x86)\videolan\vlc\vlc.exe" --qt-start-minimized --play-and-exit "c:\Program Files (x86)\videolan\vlc\Windows Exclamation.wav"

The main addition to the previous comment is --qt-start-minimized The paths and quote characters are just to illustrate a typical use. I've used this unchanged in XP, 7, 10; I expect Vista and 8.x to work too.

pol098
  • 99
  • 2
1

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.

Michael Madsen
  • 256
  • 1
  • 4
1

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

John Knoeller
  • 239
  • 1
  • 6
1

A very lightweight solution is cmndPlayWAV.jar which also works on Linux and OS/X. It has a built in sound if no parameter is specified or you can give the full path to a .wav file. Incidentally one could always make an audio message .wav to use with Windows Recorder.

budrow1138
  • 11
  • 1
  • Welcome to Super User! Please read [How do I recommend software](https://meta.superuser.com/questions/5329/how-do-i-recommend-software-in-my-answers/5330#5330) for some tips as to how you should go about recommending software. You should provide at least a link, some additional information about the software itself, and how it can be used to solve the problem in the question. – DavidPostill Dec 18 '16 at 12:20
0

You can use fmedia to play a sound file from Windows terminal:

fmedia file.mp3

This command will start the playback of file.mp3 in foreground and quit after the file has finished playing.

If you wish to do it in background, add --background switch to your command:

fmedia file.wav --background

This command will start a new process in background and detach from your console immediately.

fmedia is a portable application (works without installation) and consumes very small amount of system resources. Also, its startup time is instantaneous.

P.S. Use command fmedia.exe --install to add it to your %PATH% environment variable, otherwise you need to execute it with full path, e.g. D:\fmedia\fmedia.exe.

def
  • 61
  • 1
  • 2
0

This worked very well for me. All you have to do is change C:\Windows\Media\Windows Critical Stop.wav to whatever your file name is.

@echo off


title ERROR

::"setting up" error sound - creating vbs file to make sound
set "file=C:\Windows\Media\Windows Critical Stop.wav"
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.Controls.play
  echo do while Sound.currentmedia.duration = 0
  echo wscript.sleep 100
  echo loop
  echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
  
  ::playing sound
start /min sound.vbs

::wait 1 second for vbs to let go of file
ping n- 1 127.0.0.1>nul

::delete vbs file
del sound.vbs
0

I've found somewhere this VB script that can solve this (my apologies to author, but I don't know who it is ;). It's a bit robust but you'll get a picture:

' find VB script reference here:
' http://www.csidata.com/custserv/onlinehelp/vbsdocs/VBSTOC.htm

set objvoice = createobject("sapi.spvoice")
set objfile = createobject("sapi.spfilestream.1")

if wscript.arguments.count = 0 then
    objfile.open "c:\windows\media\Windows Proximity Notification.wav"
else 
    wscript.echo wscript.arguments(0)
end if

objvoice.volume = 100
objvoice.speakstream objfile

hope this can help, helped me 100% ...

sasho
  • 1
  • 2
0

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.

Mr. Shiny and New 安宇
  • 1,152
  • 1
  • 12
  • 22
0

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

n611x007
  • 6,336
  • 14
  • 61
  • 88
-3

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

zipizap
  • 150
  • 2
  • 1
    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. –  Jan 29 '10 at 00:31
  • 4
    This is so much not the best answer! – Antonio May 29 '17 at 14:27
  • @Antonio, that was in 2010 :) – zipizap Feb 01 '18 at 00:12
  • 3
    Amazingly, this is marked as the accepted, "best answer"! – Henrik Dec 03 '19 at 14:41
  • 5
    This answer should be deleted. It isn't even worthy to stay as a comment. – dr_ Jul 08 '20 at 12:16
  • 1
    How can this be the "best" answer? – Anic17 Nov 07 '20 at 19:39
  • 1
    @Anic17 It's not. It is marked as the "accepted" answer, because it was chosen by the person who asked the question. That does not indicate that this is the highest-scored answer. – TOOGAM Oct 02 '22 at 12:18