4

Is there a way to open a sound in VBS and without a dialogue?

This is my code...

intAnswer = _
    Msgbox("Do you want to play welcome.mp3?", _
        vbYesNo, "Play Song?")

If intAnswer = vbYes Then
    Msgbox "Opening..."
    `play %userprofile%/directory/welcome.mp3 with no dialogue`
Else
    Msgbox "Not opening..."
End If

2 Answers 2

13

You can play MP3 files in VBScript using the Windows Media Player scripting object, WMPlayer.OCX.

Dim oPlayer
Set oPlayer = CreateObject("WMPlayer.OCX")

' Play audio
oPlayer.URL = "C:\welcome.mp3"
oPlayer.controls.play 
While oPlayer.playState <> 1 ' 1 = Stopped
  WScript.Sleep 100
Wend

' Release the audio file
oPlayer.close
4
  • 1
    Sorry for the late reply but what happens if the MP3 is in the same directory as the program?
    – Carl479
    Commented Mar 15, 2014 at 8:56
  • 1
    @Carl479: Not sure I understand your question. Do you mean if it's possible to drop the folder path? Yes: oPlayer.URL = "welcome.mp3"
    – Helen
    Commented Mar 17, 2014 at 13:06
  • As in, If I have the VBS and welcome.mp3 in the same folder/directory.
    – Carl479
    Commented Mar 18, 2014 at 3:14
  • 1
    @Carl479: In this case you can specify just the file name: oPlayer.URL = "welcome.mp3"
    – Helen
    Commented Mar 18, 2014 at 5:42
0

I know that the post was released five years ago, but for anyone who's still wondering here's the code:

do

set WshShell = CreateObject("WScript.Shell")

music = "C:\example.mp3"

WshShell.Run "wmplayer """ & music & """",0,True

loop

Save it as .vbs file.

It is much simpler way and not very confusing. Works on Windows 7/10/11. Anything below i did not test.

Not the answer you're looking for? Browse other questions tagged or ask your own question.