主要通过QtMultimedia中的类实现
简单的mp3格式可以通过QMediaPlayer类实现
此外还有QSound、QAudioFormat等其他类实现其他格式的播放
ref:https://www.jianshu.com/p/917b2ea7f719
点击按钮,播放指定的音乐文件10秒
# -*- coding: utf-8 -*-
# @Time : 2020/4/17 19:06
# @Author : Zhao HL
# @File : 12_musicPlay.py
import sys,time
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import *
music_path = r'E:\12681.mp3'
class Example(QWidget):
def __init__(self):
super(Example,self).__init__()
self.init_UI()
def init_UI(self):
btn = QPushButton('play',self)
btn.resize(btn.sizeHint())
btn.move(50,50)
btn.clicked.connect(self.music_play)
self.show()
def music_play(self):
url = QUrl.fromLocalFile(music_path)
content = QMediaContent(url)
self.player = QMediaPlayer()
self.player.setMedia(content)
# self.player.setVolume(100)
self.player.play()
time.sleep(10)
self.player.stop()
if __name__ == '__main__':
app = QApplication(sys.argv)
example = Example()
sys.exit(app.exec_())