且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

出现 msgbox 时从 VBScript 中播放声音文件

更新时间:2022-06-14 22:59:32

假设您的脚本中有一个名为 Music 的文件夹,因此您可以使用这样的相对路径 ./音乐/Matrix.mp3

Assume that you have a folder named Music with your script, so you can use a relative path like this ./Music/Matrix.mp3

所以你可以试试这样:

Option Explicit
Dim Msg,Question,PathSound
Msg = "Did you want to hear some music ?"
PathSound = "./Music/Matrix.mp3" 'Relative Path
Question = MsgBox(Msg,VbQuestion+VbYesNo,Msg)
If Question = VbYes Then
    Call Play(PathSound)
Else
    Wscript.Quit()
End If
'**********************************************************
Sub Play(SoundFile)
Dim Sound
Set Sound = CreateObject("WMPlayer.OCX")
Sound.URL = SoundFile
Sound.settings.volume = 100
Sound.Controls.play
do while Sound.currentmedia.duration = 0
    wscript.sleep 100
loop
wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
End Sub
'**********************************************************

如果你喜欢在线播放音乐,你可以这样做:

And if you like to play the music online, so you can do it like this :

Option Explicit
Dim Msg,Question,PathSound
Msg = "Did you want to hear some music ?"
PathSound = "http://hackoo.alwaysdata.net/Matrix.mp3"
Question = MsgBox(Msg,VbQuestion+VbYesNo,Msg)
If Question = VbYes Then
    Call Play(PathSound)
Else
    Wscript.Quit()
End If
'**********************************************************
Sub Play(SoundFile)
Dim Sound
Set Sound = CreateObject("WMPlayer.OCX")
Sound.URL = SoundFile
Sound.settings.volume = 100
Sound.Controls.play
do while Sound.currentmedia.duration = 0
    wscript.sleep 100
loop
wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
End Sub
'**********************************************************

我希望这个答案可以帮助你完成你的主要脚本;)

I hope that this answer can help you to complete your main script ;)