I am needing to update a VBScript that runs on a wide variety of machines and plays a sound file in the background. Here is my existing code:
Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "C:\Windows\Media\Alarm01.wav"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000
When I run this code, I have recently begun getting the following error on many, but not all machines:
Line: 1
Char: 1
Error: ActiveX component can't create object: 'WMPlayer.OCX.7'
Code: 800A01AD
Source: Microsoft VBScript runtime error
I understand that this is because this object is deprecated. How is one supposed to do this these days?
I have tried running the program using the 32 bit version of cscript and wscript, but to no avail.
Your script still works on the latest Windows 11 builds, so the problem machines are configured differently than default. Maybe WMP has been uninstalled or disabled. Also check policies and AV software settings.
You may be able to get it working by re-registering wmp.dll:
regsvr32 wmp.dll
Otherwise, you may need to explore alternate solutions. Below are some options.
Option 1: PlayAudio.hta
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9">
<hta:application
showintaskbar=no
>
<script language=VBScript>
Window.ResizeTo 0,0
Window.MoveTo 999999999,999999999
</script>
</head>
<body>
<audio src="Test.mp3" autoplay>
</body>
</html>
If you can use a WAV file, you can compile a simple C# program like this:
Option 2: PlayAudio.cs
using System.Media;
class Program
{
static void Main()
{
SoundPlayer player = new SoundPlayer("Test.wav");
player.PlaySync();
}
}
Also for a WAV file, you can use PowerShell like this:
Option 3: PlayAudio.ps1
(New-Object Media.SoundPlayer "test.wav").PlaySync()
You can use RunHidden to run the PS1 script hidden.
Option 4: PlayAudio.vbs
Play WAV file via PowerShell launched from VBScript
Set oWSH = CreateObject( "WScript.Shell" )
SoundFile = "test.wav"
Command = "(New-Object Media.SoundPlayer '" & SoundFile & "').PlaySync()"
CmdLine = "Powershell.exe -Command " & Command
oWSH.Run CmdLine,0,False