2

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
Jared
  • 55
  • 2
  • 6

2 Answers2

3

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

Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • This works great! Is there any way I can make the delay/sleep shorter though? I tried messing around with both sleep functions but it gave me some wonky results (playing the sound after I clicked ok on the msgbox). As it is right now the sound is playing then several seconds pass and the msgbox appears. I would like the sound to play as close to the msgbox appearing as possible. – Jared Apr 23 '15 at 14:22
  • @Jared in this case you should upvote this answer ans accept it . For more information please read this ==> http://stackoverflow.com/tour – Hackoo Apr 24 '15 at 20:10
1

Did you mean something like that :

enter image description here

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

Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Not exactly what I'm looking for. I simply want the sound to play whenever a msgbox pops up (i.e. whenever intAnswer3 = vbyes) – Jared Apr 22 '15 at 19:07