Python 简单使用QMediaPlayer,QSoundEffect,QAudioOutput

Python 简单使用QMediaPlayer,QSoundEffect,QAudioOutput

from PyQt5 import QtCore, QtMultimedia, QtGui
import PyQt5

(1)适合于mp4,mp3
if __name__=="__main__":
app = PyQt5.QtWidgets.QApplication(sys.argv)
url = PyQt5.QtCore.QUrl.fromLocalFile("yourfilepath")
content = PyQt5.QtMultimedia.QMediaContent(url)
player = PyQt5.QtMultimedia.QMediaPlayer()
player.setMedia(content)
player.play()
sys.exit(app.exec())
(2)适合于wav
if __name__=="__main__":

effect = QtMultimedia.QSoundEffect()
effect.setSource(QUrl.fromLocalFile("./radio/7571.wav"))
effect.setLoopCount(1)
   effect.setVolume(1)
# effect.setLoopCount(QtMultimedia.QSoundEffect.Infinite)#无限循环
  effect.play()
setVolume()方法可以设置播放音频时的音量大小,参数为浮点型数值。1.0代表全音量播放,0.0代表静音;播放为 play(),停止为stop()。
(3)适合于播放PCM音频(裸流)文件,最强大的方式
if __name__=="__main__":
app = PyQt5.QtWidgets.QApplication(sys.argv)
format = PyQt5.QtMultimedia.QAudioFormat()
format.setChannelCount(1)
format.setSampleRate(1000)
format.setSampleSize(8)
format.setCodec("audio/pcm")
format.setByteOrder(QtMultimedia.QAudioFormat.LittleEndian)
format.setSampleType(QtMultimedia.QAudioFormat.UnSignedInt)
output = PyQt5.QtMultimedia.QAudioOutput(format)
rfile = PyQt5.QtCore.QFile()
rfile.setFileName("../test/19147_160913105347.adpcm")
rfile.open(PyQt5.QtCore.QIODevice.ReadOnly)
output.start(rfile)
app.exec()
posted @ 2020-08-30 19:52  萧海~  阅读(1713)  评论(0编辑  收藏  举报