检测 Windows 默认媒体播放器

3 投票
1 回答
638 浏览
提问于 2025-04-16 10:01

我想找到Windows默认媒体播放器的路径,这样我就可以在我的Python/wxPython程序中使用它。我的具体需求是列出所有的媒体文件,并用这个播放器来播放它们。

1 个回答

2

根据上面的评论,看起来你决定换个方向。不过你的问题让我很好奇,所以我还是去查了一下。

文件关联信息是存储在Windows注册表里的。要通过Python访问Windows注册表的信息,可以使用_winreg模块(这个模块在2.0及以后的版本中都有)。当前用户的文件关联信息会存储在一些特定的子键里,格式如下:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.wmv\UserChoices

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mpeg\UserChoices

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.avi\UserChoices

等等,针对你想查找的任何特定文件格式都是这样的。

这里有一个我写的小示例脚本,用来访问这些信息并把它存储为一个列表:

import _winreg as wr

# Just picked three formats - feel free to substitute or extend as needed
videoFormats = ('.wmv', '.avi', '.mpeg')

#Results written to this list
userOpenPref = []

for i in videoFormats:
    subkey = ("Software\\Microsoft\\Windows\\CurrentVersion" + 
                "\\Explorer\\FileExts\\" + i + "\\UserChoice")

    explorer = wr.OpenKey(wr.HKEY_CURRENT_USER, subkey)

    try:
        i = 0
        while 1:
            # _winreg.EnumValue() returns a tuple:
            # (subkey_name, subkey_value, subkey_type)
            # For this key, these tuples would look like this:
            # ('ProgID', '<default-program>.AssocFile.<file-type>', 1).
            # We are interested only in the value at index 1 here
            userOpenPref.append(wr.EnumValue(explorer, i)[1])
            i += 1
    except WindowsError:
        print

    explorer.Close()

print userOpenPref

输出结果:

[u'WMP11.AssocFile.WMV', u'WMP11.AssocFile.avi', u'WMP11.AssocFile.MPEG']

其中WMP11代表的是Windows Media Player 11。

希望这些信息对你有帮助。

来源:

python文档effbot教程

撰写回答