20

In Windows, is there a simple way (i.e. something you could type on a single command line) to just play a couple of .mp3 files and then exit on its own?

wmplayer, for example, does not seem to exit when finished, forcing the harried user to hunt it down and click it closed every time. (wmplayer 11 also seems to strangely repeat some files if you pass it a list.) (The older (1990's) versions of 'mplayer' used to support a '/close' command line option, but it doesn't seem to work in wmplayer 11.)

I'd prefer to use something that everyone will have on their machine (like wmplayer, quicktime...)

DVK
  • 126,886
  • 32
  • 213
  • 327
Kilo
  • 558
  • 1
  • 5
  • 13

11 Answers11

33

Old question, new answer - you could use PowerShell:

powershell -c (New-Object Media.SoundPlayer 'c:\PathTo\YourSound.wav').PlaySync();
joshcomley
  • 28,099
  • 24
  • 107
  • 147
  • 2
    Doesn't work with double quotes if you have spaces in the file path, you have to use single quotes. – Vedran Jun 25 '15 at 10:03
  • 3
    Doesn't work with mp3: _"your.mp3 is not a valid wave file."_ – xpt Sep 27 '21 at 12:24
  • It works. I played windwos Alarm.wav but after playing it gives this error as well. "Cannot process the command because of a missing parameter. A command must follow -Command." – CrackerKSR Jul 04 '22 at 18:49
12

I have used cmdmp3. Very lightweight at 28Kb.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
pathe3
  • 348
  • 2
  • 7
  • Page and download open or me. – User Sep 20 '14 at 09:57
  • 1
    I just checked the site and it's OK. The direct download for the zip file is [cmdmp3.zip](http://www.mailsend-online.com/wp/cmdmp3.zip). Unzip it and use either cmdmp3.exe or cmdmp3win.exe to play the file. The difference between them is that cmdmp3.exe uses its own window to play the file (close the window to stop) and cmdmp3win.exe just plays the file without a window. Use either cmdmp3 or cmdmp3win followed by "C:\yourfolder\yourmp3file.mp3" in order to play your file. – pathe3 Nov 17 '14 at 15:37
  • 1
    For reference if this is down again in the future, the source code can be found on [github](https://github.com/jimlawless/cmdmp3) – h2ooooooo May 18 '17 at 08:43
  • [New page link.](https://lawlessguy.wordpress.com/2015/06/27/update-to-a-command-line-mp3-player-for-windows/) [New download link.](http://www.mailsend-online.com/wp/cmdmp3new.zip) Tool works great, I'm using this on Windows to work around JavaSound's insanity. – Stefan Reich Aug 14 '17 at 13:28
3
  • There are pure command line players. Some are listed here.

  • Also, WinAmp used to have command line controller called CLAMP. I am not sure if it's still alive or available but Google for it - it was pretty good.

  • Also, VLC has some command line capabilities though I never checked them out.

DVK
  • 126,886
  • 32
  • 213
  • 327
  • 1
    I used vlc. vlc-1.1.9\vlc.exe --play-and-exit %WINDIR%\media\tada.wav – TJR May 04 '11 at 15:07
  • Note that VLC in its current version (1.1.9) is broken wrt command line arguments. You can't set the volume, and it appears that many more command line options are broken. Just quickly testing, I found that `--start-time`, `--width`, `--greyscale` are also broken (tested with video for obvious reasons). `--fullscreen` works, so it's not that command line parsing in general is broken. – MSalters May 27 '11 at 20:17
  • The "some are listed here" link is no longer valid. – whitebeard Nov 15 '16 at 15:00
  • Voted down because answer links to page with resources, but the resources have gone, which makes the answer not usable anymore. – M. le Rutte Jan 31 '17 at 09:45
  • 1
    @M.leRutte - updated link to archive.org that works. – DVK Aug 14 '17 at 13:32
  • @whitebeard - fixed – DVK Aug 14 '17 at 13:33
3

Use VBScript:

Set objArgs = Wscript.Arguments

if (objArgs.Count = 0) then
    Wscript.echo "I need a sound file as argument!"
    WScript.Quit 123
end if
Wscript.echo "Playing: " & objArgs(0) & "..."

Set objPlayer = createobject("Wmplayer.OCX.7")

With objPlayer  ' saves typing
    .settings.autoStart = True
    .settings.volume = 50  ' 0 - 100
    .settings.balance = 0  ' -100 to 100
    .settings.enableErrorDialogs = False
    .enableContextMenu = False
    .URL = objArgs(0)
    WScript.Sleep(10000)  ' time to load and start playing
    '.Controls.Pause()  ' stop
End With

MsgBox "if WMP is still playing, clicking OK will end it", _
    vbInformation, "WMP Demo finished"

If the VBScript process ends, the Media Player ends too, you have to wait for it (I don't need it, my sounds are only some seconds long).

I used this for my special case today: https://groups.google.com/forum/#!topic/microsoft.public.scripting.vbscript/gfOOvnN8t-U

Mayra Delgado
  • 531
  • 4
  • 17
  • 2
    I'd advise to end the script with `WScript.Quit 0` so that it can be used as a build step (I use it to announce succesful build). – Tomáš Zato Jan 15 '16 at 12:43
  • This works perfectly. It is too chatty for Scheduled Tasks, so I removed the `Wscript.echo` line and the final `MsgBox` And note that it is always a very good idea to give the command line a fully-qualified pathname to the .MP3 file. Excellent answer. +1 Similar to How to make the PC announce time on the hour in VBS: https://superuser.com/questions/1268702/how-to-make-windows-speak-the-time-at-the-top-of-the-hour/1268703 – SDsolar May 18 '18 at 17:39
2

This is what I did to do it:

rem Replace the following path with your music file
start C:\Users\Username\Desktop\your-music.mp3
rem Replace 10 with how many seconds you want the player to run
ping localhost -n 10 >nul
taskkill /im wmplayer.exe

Note this requires .mp3 to be associated with wmplayer.exe (Windows Media Player).

JMC
  • 21
  • 1
  • _"Note this requires .mp3 to be associated with wmplayer.exe (Windows Media Player)"_. Ah, yes! no wonder I got only a new CMD window popup. – xpt Sep 27 '21 at 12:32
2

You can use fmedia to play audio files from Windows terminal:

fmedia file1.mp3 file2.mp3

This command will start the playback of file.mp3, then file2.mp3, and then quit after the files have finished playing.

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

fmedia file.ogg --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
  • 21
  • 1
1

You can probably write a small VBScript which will use the Windows Media Player ActiveX control to play an audio file. You should be able to terminate the process from that too, once playing finished.

I'm looking around a bit and maybe can come up with a working solution. Might take a while, though.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

I am using this improve version of Mayra Delgado's answer:

Set objArgs = Wscript.Arguments

if (objArgs.Count = 0) then
    Wscript.echo "I need a sound file as argument!"
    WScript.Quit 123
end if
Wscript.echo "Playing: " & objArgs(0) & "..."

Set objPlayer = createobject("Wmplayer.OCX.7")

With objPlayer  ' saves typing
    .settings.autoStart = True
    .settings.volume = 50  ' 0 - 100
    .settings.balance = 0  ' -100 to 100
    .settings.enableErrorDialogs = False
    .enableContextMenu = False
    .URL = objArgs(0)
    ' Wait until play finish
    While .Playstate <> 1
        Wscript.Sleep 100
    Wend
End With

MsgBox "if WMP is still playing, clicking OK will end it", _
    vbInformation, "WMP Demo finished"
ngonhan
  • 21
  • 4
0

I've found that the fastest way to play .mp3 files in Windows commandline is mpeg123

I know it's not available on people's machine per default, but from my point of view, Microsoft's own players are not consistently available over different versions either.

I'm using it on a project where the execution time is essential, and features like only playing certain frames makes is very useful. I find this (in my configuration) faster than the cmdmp3 and vbscript examples mentioned in this thread.

My syntax to only play certain frames of an .mp3 file : mpg123.exe -k 2 -n 3 -q -1 -4 beep.mp3

0

Here are few scripts.

mediarunner.bat - it uses windows media player active x objects so you cannot used if there's no installed Windows Media Player (though it usually comes packed with the Windows).Accepts only one argument - the path to the file you want to play.

spplayer.bat - uses SP Voice objects but can play only .wav files.Again it accepts as only argument the path to the file you want to play.

soundplayer.bat - uses Internet Explorer objects and specific bgsound tag that can be used only in internet explorer.Can play .mp3,wav,.. files. It can accepts two arguments. The file you want to play and the sound volume (a number between -10000 to 0) :

 call soundplayer.bat "C:\Windows\Media\Windows Navigation Start.wav" 0
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

If you need something cross-platform, mplayer works well on linux and windows and is compatible with npm's play-sound.

acenturyandabit
  • 1,188
  • 10
  • 24