不同的声音播放模块不工作

发布于 2025-01-17 03:30:06 字数 3730 浏览 3 评论 0原文

我为我的妹妹创建了一个学习数学的程序,现在想添加声音。因此,查找了如何向我的程序添加声音并找到了winsound模块。我写了这段代码:

import winsound

winsound.PlaySound("victory.wav", winsound.SND_FILENAME)
stopper = input("Input something to stop the program!")

但由于某种原因它只播放默认的Windows声音。 (Bliiiiiiingg)

文件 victory.wav 与 python 脚本位于同一文件夹中。然后我看到其他人和我有同样的问题,他说添加完整路径解决了问题。于是我就这么做了:

winsound.PlaySound(r"C:\Users\Nutzer\Desktop\gui\victory.wav", winsound.SND_FILENAME)

但还是没用。所以我尝试了不同的添加路径的方法:

winsound.PlaySound("C:/Users/Nutzer/Desktop/gui/victory.wav", winsound.SND_FILENAME)
# and
winsound.PlaySound("C:\\Users\\Nutzer\\Desktop\\gui\\victory.wav", winsound.SND_FILENAME)

仍然不起作用。然后我决定切换模块,所以我安装了playsound模块,并编写了以下代码:

import playsound
playsound.playsound("victory.wav",block=True)

它说无法播放该文件,这意味着它找到了该文件。我们正在取得进展。 所以我将文件扩展名更改为 .mp3 并再次添加完整路径:

playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)

现在它说了一些不同的内容:

Error 277 for command:
        open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
    Fehler beim Starten von MCI.

    Error 305 for command:
        close "C:\Users\Nutzer\Desktop\gui\victory.mp3"
    Zusätzliche Zeichen nach einer Zeichenkette mit Anführungszeichen sind nicht erlaubt.
Failed to close the file: "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Traceback (most recent call last):
  File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 3, in <module>
    playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
    Error 277 for command:
        open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
    Fehler beim Starten von MCI.

翻译过来的意思是“启动 MCI 时出错”。

然后我决定安装 pygame 并使用 mixer 库:

import pygame.mixer as mixyy

mixyy.init()
mixyy.music.load("victory.mp3")
mixyy.music.play(loops=0)

input = input("Press enter to stop the program.")

令人惊讶的是,这确实有效。但它导致编译后的文件大小从16MB变为70MB,并且编译后的文件甚至无法打开。

所以我放弃了这个想法,找到了一个名为 mp3play 的模块,并复制了示例代码,并为我进行了调整:

import mp3play

filename = r'C:\Users\Nutzer\Desktop\gui\victory.mp3'
clip = mp3play.load(filename)

clip.play()

import time
time.sleep(min(30, clip.seconds()))
clip.stop()

现在它是这样说的:

Traceback (most recent call last):
  File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 1, in <module>
    import mp3play
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\__init__.py", line 4, in <module>
    from .windows import AudioClip as _PlatformSpecificAudioClip
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\windows.py", line 27
    print 'Error %s for "%s": %s' % (str(err), txt, buf)
          ^
SyntaxError: invalid syntax

我没有看到语法有任何问题,因为我从字面上看从他们的文档复制了示例。

我似乎无法做任何事情。 我也尝试过:

  • 使用不同的文件。
  • 使用各种文件类型。
  • 重新安装 Windows。
  • 尝试在linux上。
  • 重新启动我的电脑。
  • 在另一台计算机上尝试。
  • 正在尝试Mac。
  • 使用不同的 python 解释器。
  • 使用不同的代码可视化工具(VSC、Pycharm、Idle)。

有人可以告诉我如何解决这个问题吗?

I created a program for my little sister to learn math and now want to add sound. So looked up on how to add sound to my program and found the winsound module. I wrote this code:

import winsound

winsound.PlaySound("victory.wav", winsound.SND_FILENAME)
stopper = input("Input something to stop the program!")

But for some reason it only plays the default windows sound. (Bliiiiiingg)

The file victory.wav is located in the same folder as the python script. Then I saw someone else having the same problem as me and he said that adding the full path solved the problem. So I did that:

winsound.PlaySound(r"C:\Users\Nutzer\Desktop\gui\victory.wav", winsound.SND_FILENAME)

But it still didn't work. So I tried different methods of adding the path:

winsound.PlaySound("C:/Users/Nutzer/Desktop/gui/victory.wav", winsound.SND_FILENAME)
# and
winsound.PlaySound("C:\\Users\\Nutzer\\Desktop\\gui\\victory.wav", winsound.SND_FILENAME)

Still not working. Then I decided to switch modules, so I installed the playsound module and wrote the following code:

import playsound
playsound.playsound("victory.wav",block=True)

It said that it could not play the file, which means that it found the file. We're making progress.
So I changed the file extention to .mp3 and added the full path again:

playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)

Now it said something different:

Error 277 for command:
        open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
    Fehler beim Starten von MCI.

    Error 305 for command:
        close "C:\Users\Nutzer\Desktop\gui\victory.mp3"
    Zusätzliche Zeichen nach einer Zeichenkette mit Anführungszeichen sind nicht erlaubt.
Failed to close the file: "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Traceback (most recent call last):
  File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 3, in <module>
    playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
    Error 277 for command:
        open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
    Fehler beim Starten von MCI.

Translated it means "Error starting MCI".

Then I decided to install pygame and use the mixer libary:

import pygame.mixer as mixyy

mixyy.init()
mixyy.music.load("victory.mp3")
mixyy.music.play(loops=0)

input = input("Press enter to stop the program.")

Surprisingly, that actually worked. But it caused the compiled file size to go from 16MB to 70MB and the compiled file could not even open.

So I scrapped that idea and found a module called mp3play, and copied the example code, and adjusted it for me:

import mp3play

filename = r'C:\Users\Nutzer\Desktop\gui\victory.mp3'
clip = mp3play.load(filename)

clip.play()

import time
time.sleep(min(30, clip.seconds()))
clip.stop()

Now it said this:

Traceback (most recent call last):
  File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 1, in <module>
    import mp3play
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\__init__.py", line 4, in <module>
    from .windows import AudioClip as _PlatformSpecificAudioClip
  File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\windows.py", line 27
    print 'Error %s for "%s": %s' % (str(err), txt, buf)
          ^
SyntaxError: invalid syntax

I don't see anything wrong with the syntax, as I literally copied the example from their documentation.

I just can't seem to get anything to work.
Things I also tried:

  • Using a different file.
  • Using various filetypes.
  • Reinstalling windows.
  • Trying on linux.
  • Restarting my computer.
  • Trying on a different computer.
  • Trying on mac.
  • Using a different python interpreter.
  • Using a different code visualizer (VSC, Pycharm, Idle).

Could somebody please tell me how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢望 2025-01-24 03:30:06

实际上,您同时问了很多问题,每次只关注一个问题可能会更好。但是,由于许多问题是相关的,我将尝试在这里回答它们:

winsound

在我的系统中, winsound 似乎工作正常。以下代码对我有用:

import winsound

winsound.PlaySound('victory.wav', winsound.SND_FILENAME)

但是,音频文件必须是winsound 模块支持的有效WAVE 文件(不是MP3 文件或其他音频格式)。如果不支持,它将播放系统默认声音(您将其描述为Bliiiiingg)。可以通过传递 winsound.SND_NODEFAULT 标志:

import winsound

winsound.PlaySound('victory.wav', winsound.SND_FILENAME | winsound.SND_NODEFAULT)

在这种情况下,当尝试播放无效的文件格式时,您将收到错误 RuntimeError: Failed to play sound

不幸的是,错误消息没有指明问题的原因。传递无效路径时,您将得到与将有效路径传递到不受支持的文件时相同的错误。但是,Windows 应用程序通常支持正斜杠 ('/') 和反斜杠 ('\\')。如有疑问,请暂时将文件复制到与脚本相同的目录,并完全删除路径的目录部分,以便您可以识别问题。

请注意,WAV 格式是一种容器格式,它支持不同的编码。可能并非所有格式/编码都受支持,因此您只需尝试哪一种有效或无效。

我还尝试

playsound,它确实适用于我的一些 WAV和 MP3 文件,但不适用于所有文件。如果它不起作用,我会收到以下一个或多个错误:

playsound.PlaysoundException:
    Error 259 for command:
        play sample.mp3 wait
    The driver cannot recognize the specified command parameter.
playsound.PlaysoundException:
    Error 263 for command:
        open sample.mp3
    The specified device is not open or is not recognized by MCI.
playsound.PlaysoundException:
    Error 277 for command:
        open "sample.mp3" alias playsound_0.9221509684918991
    A problem occurred in initializing MCI.

GitHub 上有多个错误报告(请参阅问题 #36#83#113#120#121)。 Stack Overflow 上也已经出现了相关问题:

在我看来,playsound 对某些文件格式存在一些问题。可能的解决方案/解决方法包括:

  • 降级到playsound 1.2.2(python -m pip installplaysound==1.2.2)--这在某处提到过,我注意到某些文件可以确实使用 1.2.2 播放,无法使用 1.3.0 播放,但它仍然无法播放我的所有文件。
  • 使用例如 Audacity 重新编码 MP3 文件 - 这对我的不起作用文件。
  • 以其他格式保存文件(例如将 MP3 转换为 WAV)-- 这对我的文件不起作用。

请注意,任意更改文件扩展名从来都不是一个好主意。请确保文件的类型是什么,并确保它具有正确的扩展名。将 WAV 文件重命名为“.mp3”或反之亦然并不会神奇地使其可播放。

pygame

相同的 pygame 代码也对我有用。我不确定为什么 70 MB 是一个问题,或者您使用什么框架来编译代码(我假设是 py2exe 或类似的框架)。如果这确实是一个问题,您可能会问另一个与此相关的问题。可能有一种方法可以减小尺寸。

mp3play

mp3play 模块似乎已经严重过时并且不再维护。它的网站已关闭,最后一个pypi 上的发布 是在 2008 年。代码是用 Python 2.x 编写的,这就是您收到语法错误的原因。 (语法错误不在您的代码中,而是在库中。)

TL;DR

  • winsound 可以在支持的文件类型(仅限 WAV)下正常工作。
  • playsound 似乎仅适用于某些 WAV 或 MP3 文件。
  • pygame 也可以正常工作。不确定为什么尺寸是一个问题,但可能有解决方案。
  • mp3play 已经过时,不要使用它。
  • 不要随意更改文件扩展名。

You actually asked a lot of questions at the same time, and it might have been better to focus on one each time. However, since a number of questions are related, I'll try to answer them here:

winsound

At my system, winsound seems to be working fine. The following code works for me:

import winsound

winsound.PlaySound('victory.wav', winsound.SND_FILENAME)

However, the audio file must be a valid WAVE file (not an MP3 file or another audio format) that is supported by the winsound module. If it's not supported, it will play the system default sound (which you describe as Bliiiiiingg). This can be avoided by passing the winsound.SND_NODEFAULT flag:

import winsound

winsound.PlaySound('victory.wav', winsound.SND_FILENAME | winsound.SND_NODEFAULT)

In this case, when trying to play an invalid file format, you will get the error RuntimeError: Failed to play sound.

Unfortunately, the error message does not specify the cause of the problem. You will get the same error when passing an invalid path as when passing a valid path to an unsupported file. However, generally Windows applications do support forward slashes ('/') as well as backslashes ('\\'). In case of doubt, temporarily copy the file to the same directory as your script and remove the directory part of the path at all, so you can identify the problem.

Note that the WAV format is a container format, which supports different encodings. It is possible that not all formats/encodings are supported, so you just have to try which one works or not.

playsound

I also tried playsound, which did work for some of my WAV and MP3 files, but not for all of them. If it didn't work, I got one or more of the following errors:

playsound.PlaysoundException:
    Error 259 for command:
        play sample.mp3 wait
    The driver cannot recognize the specified command parameter.
playsound.PlaysoundException:
    Error 263 for command:
        open sample.mp3
    The specified device is not open or is not recognized by MCI.
playsound.PlaysoundException:
    Error 277 for command:
        open "sample.mp3" alias playsound_0.9221509684918991
    A problem occurred in initializing MCI.

There are several bug reports at GitHub (see issues #36, #83, #113, #120 and #121). There have also been questions about this on Stack Overflow already:

To me it seem that playsound has some problems with certain file formats. Possible solutions / work-arounds include:

  • Downgrade to playsound 1.2.2 (python -m pip install playsound==1.2.2) -- This was mentioned somewhere and I noticed that some files can indeed by played using 1.2.2 that cannot be played using 1.3.0, but it still doesn't play all my files.
  • Recode the MP3 file using e.g. Audacity -- This didn't work for my files.
  • Save the file in another format (e.g. convert MP3 to WAV) -- This didn't work for my files.

Note that arbitrarily changing file extensions is never a good idea. Please make sure what the type of the file is, and make sure it has the correct extension. Renaming an WAV file to '.mp3' or vice versa does not magically make it playable.

pygame

The same pygame code worked for me as well. I'm not sure why 70 MB is a problem, or what framework you use to compile the code (I assume py2exe or something similar). If this really is a problem, you might ask another question about this. There might be a way to decrease the size.

mp3play

The mp3play module seems to be heavily outdated and not maintained anymore. Its website is dead, and the last release on pypi was in 2008. The code is written in Python 2.x, and that's the reason you get the syntax error. (The syntax error is not in your code, but in the library.)

TL;DR

  • winsound works fine with supported filetypes (WAV only).
  • playsound seems to work fine with some WAV or MP3 files only.
  • pygame works fine as well. Not sure why size is an issue, but there might be solution for that.
  • mp3play is outdated, don't use it.
  • don't arbitrarily change file extensions.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文