Play sound file from within VBScript when msgbox a

2019-05-25 14:10发布

问题:

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

回答1:

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

So you can give a try like this :

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



回答2:

Did you mean something like that :

Description : This Vbscript "PlayListSongs.vbs" scan into a folder and its subfolders for songs and create a playlist in a text file in order to play it in background.

Update : I add another vbscript to stop and kill the "wscript.exe" process in order to stop music playing in background.

I add another vbscript to play the playlist with Windows Media Player in foreground.

in sum you can found into zip 3 vbscript

1- To play the playlist in the background.

2- To Stop the music.

3- To Play the music with Windows Media Player in foreground.

So you can download it from here and test it