How to play audio file on windows from command lin

2019-01-25 08:26发布

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

10条回答
叼着烟拽天下
2楼-- · 2019-01-25 08:37

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.

查看更多
甜甜的少女心
3楼-- · 2019-01-25 08:39

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

查看更多
闹够了就滚
4楼-- · 2019-01-25 08:41

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

查看更多
太酷不给撩
5楼-- · 2019-01-25 08:45

Old question, new answer - you could use PowerShell:

powershell -c (New-Object Media.SoundPlayer 'c:\PathTo\YourSound.wav').PlaySync();
查看更多
聊天终结者
6楼-- · 2019-01-25 08:47

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"
查看更多
【Aperson】
7楼-- · 2019-01-25 08:51

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

查看更多
登录 后发表回答