VBScript 对 MediaPlayer 使用过时的 ProgID

问题描述 投票:0回答:1

我需要更新一个在各种机器上运行并在后台播放声音文件的 VBScript。这是我现有的代码:

Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "%file%"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000

当我运行此代码时,我最近开始在许多(但不是所有)机器上收到以下错误:

Line: 1
Char: 1
Error: ActiveX component can't create object: 'WMPlayer.OCX.7'
Code: 800A01AD
Source: Microsoft VBScript runtime error

我知道这是因为该对象已被弃用。如今人们应该如何做到这一点?

我尝试过使用32位版本的cscript和wscript运行程序,但没有成功。

vbscript windows-media-player
1个回答
0
投票

您的脚本仍然可以在最新的 Windows 11 版本上运行,因此问题计算机的配置与默认值不同。可能 WMP 已被卸载或禁用。另请检查策略和 AV 软件设置。

您可以通过重新注册 wmp.dll 来使其工作:

regsvr32 wmp.dll

否则,您可能需要探索替代解决方案。以下是一些选项。

选项 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>

如果你可以使用 WAV 文件,你可以编译一个简单的 C# 程序,如下所示:

选项 2:PlayAudio.cs

using System.Media;

class Program
{
    static void Main()
    {
        SoundPlayer player = new SoundPlayer("Test.wav");
        player.PlaySync();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.