【问题标题】:How do I make sure .mp3 files open with Windows Media Player in Python?如何确保在 Python 中使用 Windows Media Player 打开 .mp3 文件?
【发布时间】:2017-05-26 23:55:31
【问题描述】:

我有一个程序需要在 Windows Media Player 中打开一个文件,因为文件完成后,它需要杀死 wmplayer.exe。我试过使用subprocess.Popen(["C:\Program Files (x86)\Windows Media Player\wmplayer.exe", my_file])subprocess.call(["C:\Program Files (x86)\Windows Media Player\wmplayer.exe", my_file]) 但这只会打开Windows Media Player;不是我要打开的特定文件。 (my_file 变量存储 .mp3 文件的路径(它与 .py 文件在同一个文件夹中)并且可以在我在代码中使用它以及使用 webbrowser.open 时的其他任何地方使用。

完整代码:

try:

    import webbrowser
    import os
    import time
    import sys
    import getpass
    import pip
    import subprocess
    from contextlib import contextmanager

    @contextmanager
    def suppress_stdout():
        with open(os.devnull, "w") as devnull:
            old_stdout = sys.stdout
            sys.stdout = devnull
            try:  
                yield
            finally:
                sys.stdout = old_stdout

    with suppress_stdout():
        pkgs = ['mutagen', 'gTTS']
        for package in pkgs:
            if package not in pip.get_installed_distributions():
                pip.main(['install', package])

    from gtts import gTTS
    from mutagen.mp3 import MP3

    my_file = "Text To Speech.mp3"

    username = getpass.getuser()

    def check_and_remove_file():
        if os.path.isfile(my_file):
            os.remove(my_file)

    def input_for_tts(message):
        tts = gTTS(text = input(message))
        tts.save('Text To Speech.mp3')
        subprocess.call(["C:\Program Files (x86)\Windows Media Player\wmplayer.exe", my_file])

    check_and_remove_file()

    input_for_tts("""Hello there """ + username + """. This program is
used to output the user's input as speech.
Please input something for the program to say: """)

    def text_to_speech():
        while True:
            audio = MP3(my_file)
            audio_length = audio.info.length
            time.sleep((audio_length) + 0.25)
            os.system('TASKKILL /F /IM wmplayer.exe')
            time.sleep(0.5)

            while True:
                answer = input("""
Do you want to repeat? (Y/N) """).strip().lower()
                if answer in ["yes", "y"]:
                    input_for_tts("""
Please input something for the program to say: """)
                    return text_to_speech()
                elif answer in ["no", "n"]:
                    check_and_remove_file()
                    sys.exit()
                else:
                    print("""
Sorry, I didn't understand that. Please try again with either Y or N.""")

    text_to_speech()

except KeyboardInterrupt:

    check_and_remove_file()
    print("""
Goodbye!""")
    sys.exit()

有人知道如何确保它会使用 Windows Media Player 打开该特定文件,而不仅仅是一般的 Windows Media Player?

【问题讨论】:

    标签: python windows python-3.x subprocess


    【解决方案1】:

    WMP 可能期望播放媒体的完整路径,并且不关心您的脚本从哪里执行。试试:

    wmp = r"C:\Program Files (x86)\Windows Media Player\wmplayer.exe"
    media_file = os.path.abspath(os.path.realpath(my_file))
    subprocess.call([wmp, media_file])
    

    【讨论】:

    • 需要/Open吗?
    • 据我所知。
    【解决方案2】:

    如果 windows 媒体播放器被设置为 .mp3 文件调用的默认播放器

    os.startfile(path_to_your_mp3)

    应该可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多