China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-01 22:53
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to play a sound file with batch processing? I can't find it on Baidu. View 1,849 Replies 12
Original Poster Posted 2007-06-16 12:28 ·  中国 广东 广州 黄埔区 电信
中级用户
★★
Credits 326
Posts 152
Joined 2007-05-04 06:16
19-year member
UID 87412
Gender Male
Status Offline
Just use commands to call sound files. Or VBS can also be used... Who can tell me?
Floor 2 Posted 2007-06-16 13:18 ·  中国 江西 吉安 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Using BAT as a player???
Floor 3 Posted 2007-06-16 16:48 ·  中国 安徽 合肥 电信
初级用户
Credits 109
Posts 42
Joined 2007-05-12 09:38
19-year member
UID 88253
Gender Male
Status Offline
This is something I found before, and I forgot which website it was from. It has been tested to be functional!

How to play a sound in a script?
Q:
Hi, Scripting Guy! I have a script that alerts the user of a problem by popping up a message box. Is there a way to play a sound at the same time as the message box pops up?

-- TL

A:
Hi, TL. You guys are doing this on purpose, aren't you? You just love asking questions with no clear answers. This time is the same: You can search through VBScript and WSH documentation, but you won't find any way to directly play a sound through the script, not even a "beep" sound. Or, in this case, maybe a "beep-beep" sound.

Therefore, you must call another utility to play the sound. If the sound file is in .WAV format, it's best to use Windows Sound Recorder. There are at least two reasons for this. First, Sound Recorder is relatively small and can be loaded very quickly; Media Player is much more powerful, but it's overkill for just playing a "beep-beep" sound. Second, you can run Sound Recorder implicitly by passing appropriate command-line parameters; so you (or the user) will hear the sound but won't see Sound Recorder on the screen.

The following script can play Notify.wav (a standard operating system sound file found in the Windows\Media folder):

strSoundFile = "C:\Windows\Media\Notify.wav"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True

The first two lines of this script are very simple. In the first line, we assign the file path to the strSoundFile variable; in the second line, we create an instance of the Wscript Shell object (the object that will be used to actually run Sound Recorder).

The third line is a bit more complicated. To run Sound Recorder from the command prompt, you need to type this command:

sndrec32 /play /close "C:\Windows\Media\Notify.wav"
The first two command-line parameters tell Sound Recorder to play the file and then terminate automatically; of course, the third parameter is the name of the file to play. In this example, you don't need to put double quotes around the file path; you only need to put quotes when the path name contains spaces. We prefixed with quotes to provide you with a template for when you play sound files whose path names do contain spaces. So:

sndrec32 /play /close "C:\Windows\Media\Windows XP Error.wav"
To put double quotes around this file path, we use the Chr(34) command (which inserts a double quote in the string). So this line concatenates sndrec32 /play /close, the opening double quote ("), the file C:\Windows\Media\Notify.wav, and the closing double quote ("):

strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
Finally, in the fourth line, the Run method is used, passing the command string that was just built. Parameter 0 makes Sound Recorder run in a hidden window; parameter True tells the script to wait until the sound finishes playing before continuing.

As for playing a sound at the same time as the message box pops up, you'll find that it's almost impossible; this is because Sound Recorder takes a second or two to load and start playing. It's best to do two things. First, when calling Sound Recorder, use parameter False; this will tell the script to start Sound Recorder and not wait until the sound finishes playing to continue.

Second, use the Wscript.Sleep command to pause the script for about one second; this gives Sound Recorder time to load, and hopefully, the sound will start playing around the time the message box pops up. Can it play the sound exactly at the same time as the message box pops up? Usually not. But at least it can be close to simultaneous.

Here's an example script that can also play the sound around the time the message box pops up:

strSoundFile = "C:\windows\Media\Notify.wav"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, False
Wscript.Sleep 1000
Msgbox "A problem has occurred."


Responsible Editor: Gu Hongxue
Floor 4 Posted 2007-06-17 17:25 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 906
Posts 346
Joined 2006-07-10 09:58
19-year member
UID 58334
Gender Male
Status Offline
My machine is just in sync at 100 milliseconds, heh heh
Floor 5 Posted 2007-06-17 18:17 ·  中国 北京 中国中信股份有限公司
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
With bat, you can try to directly start the sound file, and it will automatically play with the corresponding player.

With vbs, there are multiple solutions:

I. Using WMPlayer.OCX

1.1 Using URL:

Set oPlayer = CreateObject("WMPlayer.OCX")
oPlayer.URL = "D:\Music\Songs\Cyndi Wang\Rainbow Smile.mp3"
oPlayer.controls.play
Do
WScript.Sleep 100
Loop Until oPlayer.openState <> 12

WScript.Sleep oPlayer.currentMedia.duration * 1000


1.2 Using Playlist:

Set oPlayer = CreateObject("WMPlayer.OCX")
oPlayer.currentPlaylist.appendItem oPlayer.newMedia("D:\Music\Songs\Cyndi Wang\Rainbow Smile.mp3")
oPlayer.controls.play
Do
WScript.Sleep 100
Loop Until oPlayer.openState <> 12

WScript.Sleep oPlayer.currentMedia.duration * 1000


II. Using SAPI.SpFileStream

Set oVoice = CreateObject("SAPI.SpVoice")
Set oFile = CreateObject("SAPI.SpFileStream.1")

oFile.Open "c:\Windows\Media\Ding.wav"
oVoice.Speakstream oFile,1 'Note that the effect of adding or not adding the parameter 1 here
Wscript.Echo "An error occurred..."

Set oVoice = Nothing
Set oFile = Nothing


Note: This solution can only play WAV - format sounds.

In addition, using some commonly - used third - party software - provided components that can be called by VBS can also be implemented.
Floor 6 Posted 2007-06-17 21:25 ·  中国 上海 联通
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
//Using bat, you can try to directly start the sound file, and it will automatically use the corresponding player to play.

I tried start under XP SP2, but it didn't work. Only a msdos window popped up, and the sound wasn't played.
Bamboo administrator 3742668, what's the reason?

@echo off
start "C:\WINDOWS\Media\Windows XP Startup.wav"
Floor 7 Posted 2007-06-18 00:00 ·  中国 广东 广州 鹏博士宽带
初级用户
★★
Credits 176
Posts 78
Joined 2007-04-15 10:53
19-year member
UID 85268
Gender Male
Status Offline
可能是关联有问题,你查看看关联:cmd /k for /f "tokens=2 delims== %i in ('assoc .wav') do ftype %i
It may be a problem with the association. You can check the association: cmd /k for /f "tokens=2 delims== %i in ('assoc .wav') do ftype %i
Floor 8 Posted 2007-06-18 01:28 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by HAT at 2007-6-17 21:25:
//You can try to directly start the sound file with bat, which will automatically play it with the corresponding player.

I tried start under XP SP2 and it didn't work. Only a msdos window popped up, and no sound was played...

@echo off
start "" "C:\WINDOWS\Media\Windows XP Startup.wav"
Floor 9 Posted 2007-07-31 05:40 ·  中国 广东 东莞 电信
中级用户
★★
Credits 326
Posts 152
Joined 2007-05-04 06:16
19-year member
UID 87412
Gender Male
Status Offline
set mp3=c:\opera.mp3
copy /y "C:\Program Files\Windows Media Player\wmplayer.exe" %windir%\system32\
start /min wmplayer "%mp3%"
Floor 10 Posted 2007-07-31 16:16 ·  中国 四川 攀枝花 电信
初级用户
Credits 34
Posts 17
Joined 2007-06-29 21:46
19-year member
UID 92719
Gender Male
From 介休
Status Offline
Hehe, learned something, thanks to all the experts
Floor 11 Posted 2007-08-01 06:28 ·  中国 广东 东莞 电信
中级用户
★★
Credits 326
Posts 152
Joined 2007-05-04 06:16
19-year member
UID 87412
Gender Male
Status Offline
Set oPlayer = CreateObject("WMPlayer.OCX")
oPlayer.URL = "d:\有一种爱叫做放手.mp3"
oPlayer.controls.play
Do
WScript.Sleep 100
Loop Until oPlayer.openState <> 12

WScript.Sleep oPlayer.currentMedia.duration * 1000
Floor 12 Posted 2007-08-02 22:57 ·  中国 安徽 马鞍山 电信
初级用户
Credits 20
Posts 11
Joined 2007-08-02 21:06
18-year member
UID 94527
Gender Male
Status Offline
You can play music under DOS with mplayer
mplayer.exe "C:\Documents and Settings\Administrator\My Documents\My Music\Friend.mp3"

I have this software in my cloud disk
http//ngd.ys168.com
Floor 13 Posted 2010-01-05 07:45 ·  中国 广东 深圳 联通
初级用户
少帅
Credits 42
Posts 24
Joined 2009-12-17 20:53
16-year member
UID 156814
Gender Male
From 深圳
Status Offline
Forum Jump: