使用Pyqt4和QSound播放声音 [英] Playing sound with Pyqt4 and QSound

查看:456
本文介绍了使用Pyqt4和QSound播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了简短起见,经过数小时的寻找在pyqt4中播放声音的方法后,我仍然不明白为什么这个简单的小代码无法正常工作?我没有收到任何错误消息,但没有声音(我尝试过mp3,ogg和wav),但是我只是得到了一些Windows的"bing"声音,但没有我真正想要的声音文件.我知道有声子,但是出于各种原因,我真的很想使用QSound,还因为它应该更简单.如果您能给我任何提示为什么不起作用或使用QSound给出工作代码的示例,我将非常感激.

To keep it short, after hours of looking for a way to play sound in pyqt4, I still can't figure out why this simple little code wouldn't work? I get no error message or anything, but instead of the sound (I've tried mp3, ogg and wav) I just get a little windows "bing" sound but not the sound file I want actually want. I know there's phonon, but I really want to use QSound for various reasons, also because it's supposed to be much simpler. If you could give me any hint to why this isn't working or give an example of working code using QSound, I'd be extremely thankful.

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QSound

class Example(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.initUI()

    def initUI(self):

        self.setGeometry(300,300,200,200)

        self.b1 = QtGui.QPushButton("Play", self)
        self.b1.clicked.connect(self.Play)
        self.b1.move(50, 80)


    def Play(self):

        QSound.play("C:\directory\b1.mp3")




def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

推荐答案

您在这里可能有两个不同的问题.

You may have two different problems here.

首先,如果要使用Windows样式的路径名,则需要使用原始字符串或反斜杠转义符.在常规字符串中,\b是单个退格字符,而不是后跟b\.换句话说,当您确实要播放文件C:\\directory\\b1.mp3时,您正在尝试播放文件'C:\ directory \ x081.mp3'.请参阅文档中的字符串和字面量以了解细节.

First, if you want to use Windows-style pathnames, you need to use either raw strings or backslash escapes. In a regular string, \b is a single backspace character, not a \ followed by a b. In other words, you're trying to play the file 'C:\directory\x081.mp3', when you really want to play the file C:\\directory\\b1.mp3. See String and Bytes literals in the docs for details.

您可以尝试记住所有特殊的反斜杠转义字符,但是更简单的解决方案是始终转义所有反斜杠.或者,甚至更简单地,将原始字符串用于所有Windows样式的路径名.或者,更简单的是,如果您使用的函数甚至在Windows上也允许POSIX样式的路径名,则只需使用它们即可.换句话说,是以下之一:

You can try to memorize all of the special backslash-escape characters, but the simpler solution is to just always escape all backslashes. Or, even simpler, use raw strings for all Windows-style pathnames. Or, even simpler, if the function you're using allows POSIX-style pathnames even on Windows, just use them. In other words, one of the following:

"C:\directory\\b1.mp3"
"C:\\directory\\b1.mp3"
r"C:\directory\b1.mp3"
"C:/directory/b1.mp3"


最重要的是,如果您使用的是较旧版本的PyQt,则不能以这种方式调用静态Qt方法.换句话说,您不是在调用QSound::play(filename)静态方法,而是在QSound::play()实例(插槽)方法.在更高的版本中,这应该没问题-尽管如文档所述,构造一个QSound并调用其play方法可能……立即播放更多",所以您还是想这样做.


On top of that, if you're using an older version of PyQt, you can't call static Qt methods that way. In other words, you're not calling the QSound::play(filename) static method, but the QSound::play() instance (slot) method. In later versions, this should be fine—although, as the documentation says, constructing a QSound and calling its play method "may… play more immediately", so you may want to do it anyway.

如果您有此问题,解决方法是:

If you have this problem, the way to fix it is:

QSound(r"C:\directory\b1.mp3").play()

尽管在实际代码中,您可能希望保留大多数QSound对象,而不是每次都重新创建它们.

Although in realistic code, you may want to keep most of your QSound objects around instead of re-creating them each time.

在评论中,您说:

我发现QSound实际上不支持资源.不幸的是,我不知道这意味着什么,也许您可​​以解释一下对我的代码意味着什么.

I found out that QSound actually does not support resources. Unfortunately I have no clue what that means, maybe you could explain what that means for my code.

这对您的代码没有任何意义,至少在今天没有.您已将声音保存在常规文件中,并尝试通过绝对路径名访问这些文件.

It doesn't mean anything for your code, at least not today. You've got your sounds in regular files, and you're trying to access those files by absolute pathname.

但是,如果您想将来改进应用程序,可能会遇到麻烦.

But if you want to improve your app in the future, it might get in the way.

Qt资源系统中对资源进行了说明. .这个想法是,您没有像图像等那样拥有一堆单独的文件,而只是拥有一个像MyApp.exe这样的可执行文件,而所有资源文件都挤在了该可执行文件中.在运行时,您仍然可以访问这些资源,就像它们仍然是单独的文件一样.您只需使用:/sounds/b1.mp3之类的特殊路径(或者更好的是,从路径名切换为URL,并使用诸如qrc:///sounds/b1.mp3之类的特殊URL).

Resources are explained in The Qt Resource System. The idea is that, instead of having a bunch of separate files for images, etc., you just have one executable file like MyApp.exe, with all of the resource files crammed into that executable. At runtime, you can still access these resources almost as if they were still separate files. You just use special paths like :/sounds/b1.mp3 (or, better, switch from pathnames to URLs, and use special URLs like qrc:///sounds/b1.mp3).

踢球者是那个几乎".某些功能无法直接在资源上使用,因此,如果要使用这些功能,则必须将资源显式提取到临时文件中.

The kicker is that "almost". A few features don't work directly on resources, so you have to explicitly extract a resource to a temporary file if you want to use those features.

这篇关于使用Pyqt4和QSound播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆