subclass QMediaPlayer

This commit is contained in:
billypom on debian 2025-04-19 08:15:47 -04:00
parent 89804aaccb
commit c3945c51b8
4 changed files with 44 additions and 3 deletions

26
components/MediaPlayer.py Normal file
View File

@ -0,0 +1,26 @@
from PyQt5.QtCore import QObject, QUrl
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QMediaPlaylist
from PyQt5.QtWidgets import QApplication
import sys
class MediaPlayer(QMediaPlayer):
def __init__(self):
super().__init__()
# Connect mediaStatusChanged signal to our custom function
self.mediaStatusChanged.connect(self.on_media_status_changed)
# def play(self, file_path):
# media_content = QMediaContent(QUrl.fromLocalFile(file_path))
# self.player.setMedia(media_content)
# self.player.play()
def on_media_status_changed(self, status):
if status == QMediaPlayer.MediaStatus.EndOfMedia:
print("Song ended, triggering custom function!")
self.on_song_ended()
def on_song_ended(self):
# Your custom logic when the song ends
print("Custom function executed after song ended!")

View File

@ -66,6 +66,7 @@ from configparser import ConfigParser
class MusicTable(QTableView): class MusicTable(QTableView):
playlistStatsSignal = pyqtSignal(str) playlistStatsSignal = pyqtSignal(str)
loadMusicTableSignal = pyqtSignal()
playPauseSignal = pyqtSignal() playPauseSignal = pyqtSignal()
playSignal = pyqtSignal(str) playSignal = pyqtSignal(str)
enterKey = pyqtSignal() enterKey = pyqtSignal()
@ -698,7 +699,7 @@ class MusicTable(QTableView):
# spawn new thread and calculate myself? # spawn new thread and calculate myself?
total_time: int = 0 # total time of all songs in seconds total_time: int = 0 # total time of all songs in seconds
for row_data in data: for row_data in data:
print(row_data) # print(row_data)
# if "length" in fields: # if "length" in fields:
row_count += 1 row_count += 1
id, *rest_of_data = row_data id, *rest_of_data = row_data
@ -720,6 +721,7 @@ class MusicTable(QTableView):
self.model2.layoutChanged.emit() # emits a signal that the view should be updated self.model2.layoutChanged.emit() # emits a signal that the view should be updated
self.playlistStatsSignal.emit(f"Songs: {row_count} | Total time: {total_time}") self.playlistStatsSignal.emit(f"Songs: {row_count} | Total time: {total_time}")
self.loadMusicTableSignal.emit()
self.connect_data_changed() self.connect_data_changed()
self.connect_layout_changed() self.connect_layout_changed()

View File

@ -11,3 +11,4 @@ from .PlaylistsPane import PlaylistsPane
from .ExportPlaylistWindow import ExportPlaylistWindow from .ExportPlaylistWindow import ExportPlaylistWindow
from .QuestionBoxDetails import QuestionBoxDetails from .QuestionBoxDetails import QuestionBoxDetails
from .HeaderTags import HeaderTags from .HeaderTags import HeaderTags
from .MediaPlayer import MediaPlayer

16
main.py
View File

@ -50,6 +50,7 @@ from utils import (
set_album_art, set_album_art,
) )
from components import ( from components import (
MediaPlayer,
PreferencesWindow, PreferencesWindow,
AudioVisualizer, AudioVisualizer,
CreatePlaylistWindow, CreatePlaylistWindow,
@ -168,8 +169,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
# widget bits # widget bits
self.album_art_scene: QGraphicsScene = QGraphicsScene() self.album_art_scene: QGraphicsScene = QGraphicsScene()
self.player: QMediaPlayer = QMediaPlayer() # Audio player object # self.player: QMediaPlayer = QMediaPlayer() # Audio player object
self.player: QMediaPlayer = MediaPlayer()
self.playlist: QMediaPlaylist = QMediaPlaylist() self.playlist: QMediaPlaylist = QMediaPlaylist()
# set index on choose song
# index is the model2's row number? i guess?
self.probe: QAudioProbe = QAudioProbe() # Gets audio buffer data self.probe: QAudioProbe = QAudioProbe() # Gets audio buffer data
self.audio_visualizer: AudioVisualizer = AudioVisualizer( self.audio_visualizer: AudioVisualizer = AudioVisualizer(
self.player, self.probe, self.PlotWidget self.player, self.probe, self.PlotWidget
@ -253,6 +257,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.tableView.playlistStatsSignal.connect( self.tableView.playlistStatsSignal.connect(
self.set_permanent_status_bar_message self.set_permanent_status_bar_message
) )
self.tableView.loadMusicTableSignal.connect(self.load_media_playlist)
self.tableView.load_music_table() self.tableView.load_music_table()
# playlistTreeView # playlistTreeView
@ -363,6 +368,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
# | | # | |
# |____________________| # |____________________|
def load_media_playlist(self):
self.proxymodel.row
pass
# self.playlist.
def setup_fonts(self): def setup_fonts(self):
"""Initializes font sizes and behaviors for various UI components""" """Initializes font sizes and behaviors for various UI components"""
font: QFont = QFont() font: QFont = QFont()
@ -421,7 +431,9 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
def play_audio_file(self, filepath=None) -> None: def play_audio_file(self, filepath=None) -> None:
""" """
Start playback of `tableView.current_song_filepath` & moves playback slider Start playback of filepath & moves playback slider
filepath default value = `tableView.current_song_filepath`
""" """
if not filepath: if not filepath:
filepath = self.tableView.get_selected_song_filepath() filepath = self.tableView.get_selected_song_filepath()