2017-05-26 4 views
-1

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を使用すると)。Windows Media PlayerでPythonで.mp3ファイルを開く方法を教えてください。

全コード:誰もが、それは一般的にはちょうどWindows Media PlayerをWindows Media Playerをして​​、その特定のファイルを開いて、ないだろうことを確認する方法を

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() 

を知っていますか?

答えて

0

おそらく、メディアへのフルパスが再生され、スクリプトがどこから実行されているかはわかりません。試してみてください:

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

'/ Open'は必要ですか? – Marichyasana

+0

私の知る限りではありません。 – zwer

-1

Windowsメディアプレーヤーが

os.startfile(path_to_your_mp3)

を呼び出す.mp3ファイルが動作するはずのためにあなたのデフォルトのプレーヤーとして設定されている場合。

関連する問題