Search code examples
vbscript

VBS Play Sound With no Dialogue


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

Solution

  • 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