如何从命令行在 Windows 上播放音频文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1569765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play audio file on windows from command line?
提问by Kilo
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?
在 Windows 中,是否有一种简单的方法(即您可以在单个命令行上键入的内容)来播放几个 .mp3 文件然后自行退出?
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.)
例如,wmplayer 在完成时似乎并没有退出,迫使忙碌的用户每次都找到它并单击它关闭。(如果您将列表传递给 wmplayer 11,它似乎也会奇怪地重复一些文件。)(较旧的(1990 年代)版本的 'mplayer' 用于支持 '/close' 命令行选项,但它似乎不适用于wmplayer 11.)
I'd prefer to use something that everyone will have on their machine (like wmplayer, quicktime...)
我更喜欢使用每个人都会在他们的机器上拥有的东西(比如 wmplayer、quicktime ......)
回答by joshcomley
Old question, new answer - you could use PowerShell:
老问题,新答案 - 您可以使用 PowerShell:
powershell -c (New-Object Media.SoundPlayer 'c:\PathTo\YourSound.wav').PlaySync();
回答by Mayra Delgado
Use VBScript:
使用 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).
如果 VBScript 进程结束,媒体播放器也结束,您必须等待它(我不需要它,我的声音只有几秒钟长)。
I used this for my special case today: https://groups.google.com/forum/#!topic/microsoft.public.scripting.vbscript/gfOOvnN8t-U
我今天将此用于我的特殊情况:https: //groups.google.com/forum/#!topic/microsoft.public.scripting.vbscript/gfOOvnN8t-U
回答by DVK
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 capabilitiesthough I never checked them out.
回答by JMC
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).
请注意,这需要将 .mp3 与 wmplayer.exe (Windows Media Player) 相关联。
回答by ngonhan
I am using this improve version of Mayra Delgado's answer:
我正在使用 Mayra Delgado 答案的改进版本:
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"
回答by Stig Meyer Jensen
I've found that the fastest way to play .mp3 files in Windows commandline is mpeg123
我发现在 Windows 命令行中播放 .mp3 文件的最快方法是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.
我知道默认情况下它在人们的机器上不可用,但从我的角度来看,Microsoft 自己的播放器在不同版本上也并非始终可用。
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.
我在一个执行时间至关重要的项目中使用它,并且诸如仅播放某些帧之类的功能非常有用。我发现这(在我的配置中)比此线程中提到的 cmdmp3 和 vbscript 示例更快。
My syntax to only play certain frames of an .mp3 file : mpg123.exe -k 2 -n 3 -q -1 -4 beep.mp3
我的语法只播放 .mp3 文件的某些帧: mpg123.exe -k 2 -n 3 -q -1 -4 beep.mp3
回答by npocmaka
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.
mediarunner.bat- 它使用 windows media player active x 对象,因此如果没有安装 Windows Media Player,则无法使用(尽管它通常与 Windows 一起打包)。仅接受一个参数 - 要播放的文件的路径。
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.
spplayer.bat- 使用 SP Voice 对象,但只能播放 .wav 文件。同样,它只接受您要播放的文件的路径作为参数。
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) :
soundplayer.bat- 使用 Internet Explorer 对象和只能在 Internet Explorer 中使用的特定 bgsound 标签。可以播放 .mp3、wav、.. 文件。它可以接受两个参数。您要播放的文件和音量(-10000 到 0 之间的数字):
call soundplayer.bat "C:\Windows\Media\Windows Navigation Start.wav" 0
回答by def
You can use fmediato play audio files from Windows terminal:
您可以使用fmedia从 Windows 终端播放音频文件:
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.
此命令将开始播放file.mp3
,然后file2.mp3
,然后在文件播放完毕后退出。
If you wish to do it in background, add --background
switch to your command:
如果您希望在后台执行此操作,请将--background
switch添加到您的命令中:
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.
fmedia 是一个可移植的应用程序(无需安装即可运行),占用的系统资源非常少。此外,它的启动时间是瞬时的。
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
PS使用命令fmedia.exe --install
将其添加到您的%PATH%
环境变量中,否则您需要使用完整路径执行它,例如D:\fmedia\fmedia.exe
回答by Joey
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.
您可能可以编写一个小的 VBScript,它将使用 Windows Media Player ActiveX 控件来播放音频文件。一旦播放完成,您也应该能够从中终止该过程。
I'm looking around a bit and maybe can come up with a working solution. Might take a while, though.
我环顾四周,也许可以想出一个可行的解决方案。不过可能需要一段时间。