当出现msgbox时,从VBScript中播放声音文件 [英] Play sound file from within VBScript when msgbox appears

查看:181
本文介绍了当出现msgbox时,从VBScript中播放声音文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图显示某个msgbox时,我正在尝试从VBScript中播放声音文件.唯一的问题是,我将把它发送到其他地方,接收它的人将没有与我要播放的音频文件相同的路径名.我当时正在考虑将所有要使用的声音文件与脚本放在同一文件夹中,然后发送该文件夹,但是我不知道如何确保声音文件能够播放.

I am trying to play a sound file from within a VBScript when a certain msgbox appears. The only problem is that I will be sending this elsewhere and the person who receives it won't have the same pathname as the audio file that I want to play. I was thinking about putting all of the sound files that I want to use in the same folder as the script and then sending that folder, but I don't know how to make sure the sound file will play.

所以我想最大的问题是如何推广路径名,以便任何人都可以从任何计算机上的脚本中听到文件.

So I guess the biggest question is how to generalize the pathname so that anyone can hear the file from within the script from any machine.

到目前为止,这是我的代码:

Here is my code so far:

    if intAnswer3 = vbyes then
        strSoundFile = "C:\pathname"
        Set objShell = CreateObject("Wscript.Shell")
        strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
        objShell.Run strCommand, 0, True

推荐答案

假定脚本中有一个名为 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 ;)

这篇关于当出现msgbox时,从VBScript中播放声音文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆