简体   繁体   English

Pyinstaller 是否能够将 .py 文件与音频一起转换为 exe?

[英]Is Pyinstaller able to convert .py file to exe together with audio?

I created a virtual assistant which can search for things for me online, open specific websites, open applications like word, powerpoint, etc.. using Python 3.6 on Windows 10. I have successfully converted it into .exe file and it's running smoothly BUT it doesn't play sound clips I included with my code.我创建了一个虚拟助手,它可以为我在线搜索内容,打开特定网站,打开 word、powerpoint 等应用程序等。在 Windows 10 上使用 Python 3.6。我已成功将其转换为 .exe 文件并且运行流畅但它不播放我的代码中包含的声音片段。 I put the sounds on the same directory as my code before I converted it to exe.在将其转换为 exe 之前,我将声音放在与代码相同的目录中。 Instead of playing the sound, it was replaced by the sound of windows when you are trying to run an application as ADMINISTRATOR (i hope this does make sense).当您尝试以管理员身份运行应用程序时,它没有播放声音,而是被 Windows 声音取代(我希望这确实有意义)。 Is there any way I can fix this or can Pyinstaller mix my code with the audio?有什么办法可以解决这个问题,或者 Pyinstaller 可以将我的代码与音频混合吗? Thanks for any help!谢谢你的帮助!

I've never tried anything aside from re-converting it because removing the audio file from the directory won't make any sense.除了重新转换它之外,我从未尝试过任何其他方法,因为从目录中删除音频文件没有任何意义。

this is the part of my code that plays sounds.这是我的代码中播放声音的部分。 if anyone of you wants/needs to see my entire code (it has 500 lines) just tell me and I will provide it.如果你们中的任何人想要/需要查看我的整个代码(它有 500 行),请告诉我,我会提供。

    elif "toss a coin" in recog1.recognize_google(audio, language="en-US"):
        coin = ["HEADS", "TAILS"]
        engine.say("Tossing a coin...")
        engine.runAndWait()
        winsound.PlaySound("coin_toss.wav", winsound.SND_FILENAME)
        engine.say("The coin toss shows" + random.choice(coin))
        engine.runAndWait()

    elif "roll a die" in recog1.recognize_google(audio, language="en-US"):
        engine.say("Rolling a die")
        engine.runAndWait()
        winsound.PlaySound("roll_die", winsound.SND_FILENAME)
        engine.say("The die result shows " + str(random.randint(0, 6)))
        engine.runAndWait()

when I run this on my IDE (Pycharm) it works flawlessly but when I run it on the command line, it runs too but the audio doesn't work.当我在我的 IDE (Pycharm) 上运行它时,它完美无缺,但是当我在命令行上运行它时,它也会运行,但音频不起作用。

PyInstaller won't do such a thing. PyInstaller 不会做这样的事情。 You need to provide your sound files manually.您需要手动提供您的声音文件。 First, you need to add your sound files to the output executable with add-data flag then create a function to load your files from the extracted path.首先,您需要使用add-data标志将声音文件添加到输出可执行文件中,然后创建一个函数以从提取的路径加载文件。

Remember that I'm loading sound files with resource_path function.请记住,我正在使用resource_path函数加载声音文件。 Put song files next to your script file.将歌曲文件放在脚本文件旁边。

import os
import sys

def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)


...
elif "toss a coin" in recog1.recognize_google(audio, language="en-US"):
    coin = ["HEADS", "TAILS"]
    engine.say("Tossing a coin...")
    engine.runAndWait()
    winsound.PlaySound(resource_path("coin_toss.wav"), winsound.SND_FILENAME)
    engine.say("The coin toss shows" + random.choice(coin))
    engine.runAndWait()

elif "roll a die" in recog1.recognize_google(audio, language="en-US"):
    engine.say("Rolling a die")
    engine.runAndWait()
    winsound.PlaySound(resource_path("roll_die"), winsound.SND_FILENAME)
    engine.say("The die result shows " + str(random.randint(0, 6)))
    engine.runAndWait()
...

And generate your executable with:并使用以下命令生成可执行文件:

pyinstaller -F --add-data "path/to/coin_toss.wav;." --add-data "path/to/roll_die;." script.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法使用 pyinstaller 将我的 python 文件转换为 exe - I'm not able to convert my python file into a exe using pyinstaller 问题:无法使用 pyinstaller、py2exe 将 .py 文件转换为 exe - problem : cant convert .py file to exe , using pyinstaller, py2exe 无法将我的 py 文件转换为 exe(尝试了 auto-py-to-exe 和 pyinstaller) - Unable to convert my py file to exe (tried auto-py-to-exe and pyinstaller) 无法使用 pyinstaller 将 py 文件转换为 exe - Unable to convert py files to exe using pyinstaller 无法使用 pyinstaller 将.py 转换为 .exe - cant convert .py to .exe using pyinstaller 使用 pyinstaller 将 .py 转换为 .exe 文件 - Converting .py to .exe file using pyinstaller 如何从 PyInstaller .exe 恢复 .py 文件? - How to recover a .py file from PyInstaller .exe? 当我使用 Pyinstaller 将 .py 文件转换为 .exe 时,如何包含输出文件? - How do I include an output file when I convert a .py file to .exe using Pyinstaller? 我正在尝试通过 pyinstaller 将 .py 文件转换为 .exe 文件,但出现此警告 - I'm trying to convert a .py file into .exe file by pyinstaller but this warning showed up pyinstaller 不顺利我能知道一些其他方法将.py 文件转换为.exe - pyinstaller is not going well can I know some other ways to convert .py file to .exe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM