Я пытаюсь разобраться с модулем QMultiMedia()
в PyQt5.
Я хочу воспроизвести трек, но он попросту не воспроизводится,
хотя я соединил его со слотом play()
.
Может быть тут есть люди, знающие этот модуль?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtGui import *
from PyQt5.QtMultimediaWidgets import *
from PyQt5.QtCore import *
from MainWindow import Ui_MainWindow
from os.path import expanduser
class PlayListModel(QAbstractItemModel):
Title, ColumnCount = range(2)
def __init__(self, parent=None):
super(PlayListModel, self).__init__(parent)
self.m_playlist = None
def rowCount(self, parent=QModelIndex()):
return self.m_playlist.mediaCount() if self.m_playlist is not None and not parent.isValid() else 0
def columnCount(self, parent=QModelIndex()):
return self.ColumnCount if not parent.isValid() else 0
def index(self, row, column, parent=QModelIndex()):
return self.createIndex(row, column) if \
self.m_playlist is not None and not parent.isValid() \
and row >= 0 and row < self.m_playlist.mediaCount() \
and 0 <= column < self.ColumnCount else QModelIndex()
def parent(self, child):
return QModelIndex()
def data(self, index, role=Qt.DisplayRole):
if index.isValid() and role == Qt.DisplayRole:
if index.column() == self.Title:
location = self.m_playlist.media(index.row()).canonicalUrl()
return QFileInfo(location.path()).fileName()
return self.m_data[index]
return None
def playlist(self):
return self.m_playlist
def setPlaylist(self, playlist):
if self.m_playlist is not None:
self.m_playlist.mediaAboutToBeInserted.disconnect(
self.beginInsertItems)
self.m_playlist.mediaInserted.disconnect(self.endInsertItems)
self.m_playlist.mediaAboutToBeRemoved.disconnect(
self.beginRemoveItems)
self.m_playlist.mediaRemoved.disconnect(self.endRemoveItems)
self.m_playlist.mediaChanged.disconnect(self.changeItems)
self.beginResetModel()
self.m_playlist = playlist
if self.m_playlist is not None:
self.m_playlist.mediaAboutToBeInserted.connect(
self.beginInsertItems)
self.m_playlist.mediaInserted.connect(self.endInsertItems)
self.m_playlist.mediaAboutToBeRemoved.connect(
self.beginRemoveItems)
self.m_playlist.mediaRemoved.connect(self.endRemoveItems)
self.m_playlist.mediaChanged.connect(self.changeItems)
self.endResetModel()
def beginInsertItems(self, start, end):
self.beginInsertRows(QModelIndex(), start, end)
def endInsertItems(self):
self.endInsertRows()
def beginRemoveItems(self, start, end):
self.beginRemoveRows(QModelIndex(), start, end)
def endRemoveItems(self):
self.endRemoveRows()
def changeItems(self, start, end):
self.dataChanged.emit(self.index(start, 0),
self.index(end, self.ColumnCount))
class MyWidget(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.player = QMediaPlayer()
self.playlist = QMediaPlaylist()
self.player.setVolume(70)
self.buttonPlay.clicked.connect(self.player.play)
self.action_3.triggered.connect(self.open_file)
self.player.setVolume(70)
self.model = PlayListModel()
self.model.setPlaylist(self.playlist)
self.listTracks.setModel(self.model)
self.listTracks.setCurrentIndex(
self.model.index(self.playlist.currentIndex(), 0)
)
def open_file(self):
fileChoosen = QFileDialog.getOpenFileUrl(self, 'Open Music File',
expanduser('~'),
'Audio (*.mp3 *.ogg *.wav)',
'*.mp3 *.ogg *.wav')
if fileChoosen != None:
self.playlist.addMedia(QMediaContent(fileChoosen[0]))
def my_excepthook(type, value, tback):
QtWidgets.QMessageBox.critical(
window, "CRITICAL ERROR", str(value),
QtWidgets.QMessageBox.Cancel
)
sys.__excepthook__(type, value, tback)
sys.excepthook = my_excepthook
app = QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())