QAudioOutput 不播放声音
QAudioOutput not playing sound
我有一个非常简单的示例,尝试使用 PySide2 (Qt 5.15) 在 Python3 中使用 QAudioOutput class 播放声音。我没有听到计算机发出任何声音。
#!/bin/python3
from PySide2.QtMultimedia import QAudioDeviceInfo, QAudioOutput, QAudioFormat
from PySide2.QtCore import QFile, QIODevice
sourceFile = QFile()
sourceFile.setFileName("/home/ubuntu/sound.wav");
sourceFile.open(QIODevice.ReadOnly);
format = QAudioFormat()
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat.Endian.LittleEndian);
format.setSampleType(QAudioFormat.SampleType.UnSignedInt);
info = QAudioDeviceInfo.defaultOutputDevice();
if (info.isFormatSupported(format) != True):
qWarning() << "Raw audio format not supported by backend, cannot play audio.";
audio = QAudioOutput(info, format);
audio.start(sourceFile);
输出:
ubuntu@ubuntu:~$ python3 test.py
QObject::startTimer: Timers can only be used with threads started with QThread
Qt 的许多元素都需要一个事件循环,QAudioOutput 就是这种情况,因为它异步运行:
import sys
from PySide2.QtMultimedia import QAudioDeviceInfo, QAudioOutput, QAudioFormat
from PySide2.QtCore import QFile, QIODevice, QCoreApplication
def main():
app = QCoreApplication()
sourceFile = QFile("/home/ubuntu/sound.wav")
if not sourceFile.open(QIODevice.ReadOnly):
print(sourceFile.errorString())
sys.exit(-1)
fmt = QAudioFormat()
fmt.setSampleRate(8000)
fmt.setChannelCount(1)
fmt.setSampleSize(8)
fmt.setCodec("audio/pcm")
fmt.setByteOrder(QAudioFormat.Endian.LittleEndian)
fmt.setSampleType(QAudioFormat.SampleType.UnSignedInt)
info = QAudioDeviceInfo.defaultOutputDevice()
if not info.isFormatSupported(fmt):
print("Raw audio fmt not supported by backend, cannot play audio.")
sys.exit(-1)
audio = QAudioOutput(info, fmt)
audio.start(sourceFile)
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
我有一个非常简单的示例,尝试使用 PySide2 (Qt 5.15) 在 Python3 中使用 QAudioOutput class 播放声音。我没有听到计算机发出任何声音。
#!/bin/python3
from PySide2.QtMultimedia import QAudioDeviceInfo, QAudioOutput, QAudioFormat
from PySide2.QtCore import QFile, QIODevice
sourceFile = QFile()
sourceFile.setFileName("/home/ubuntu/sound.wav");
sourceFile.open(QIODevice.ReadOnly);
format = QAudioFormat()
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat.Endian.LittleEndian);
format.setSampleType(QAudioFormat.SampleType.UnSignedInt);
info = QAudioDeviceInfo.defaultOutputDevice();
if (info.isFormatSupported(format) != True):
qWarning() << "Raw audio format not supported by backend, cannot play audio.";
audio = QAudioOutput(info, format);
audio.start(sourceFile);
输出:
ubuntu@ubuntu:~$ python3 test.py
QObject::startTimer: Timers can only be used with threads started with QThread
Qt 的许多元素都需要一个事件循环,QAudioOutput 就是这种情况,因为它异步运行:
import sys
from PySide2.QtMultimedia import QAudioDeviceInfo, QAudioOutput, QAudioFormat
from PySide2.QtCore import QFile, QIODevice, QCoreApplication
def main():
app = QCoreApplication()
sourceFile = QFile("/home/ubuntu/sound.wav")
if not sourceFile.open(QIODevice.ReadOnly):
print(sourceFile.errorString())
sys.exit(-1)
fmt = QAudioFormat()
fmt.setSampleRate(8000)
fmt.setChannelCount(1)
fmt.setSampleSize(8)
fmt.setCodec("audio/pcm")
fmt.setByteOrder(QAudioFormat.Endian.LittleEndian)
fmt.setSampleType(QAudioFormat.SampleType.UnSignedInt)
info = QAudioDeviceInfo.defaultOutputDevice()
if not info.isFormatSupported(fmt):
print("Raw audio fmt not supported by backend, cannot play audio.")
sys.exit(-1)
audio = QAudioOutput(info, fmt)
audio.start(sourceFile)
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()