Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5 Music Player
#1
Still plan on tweaking and trying to shorten the code but, here is what I have working for now.

#! /usr/bin/env python3

from functools import partial
import sys
from PyQt5.QtCore import Qt, QUrl, QDir
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QGridLayout, \
QFrame, QGraphicsDropShadowEffect, QGraphicsView, QGraphicsScene, QLabel, \
QPushButton, QHBoxLayout, QStyle, QListWidget, QFileDialog
from PyQt5.QtGui import QGradient, QFont, QColor, QCursor, QIcon
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QMediaPlaylist


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('PyQt Music Player')
        self.player = QMediaPlayer()
        self.playlist = QMediaPlaylist(self.player)
        self.player.setPlaylist(self.playlist)
        self.url = QUrl()

        # Setup the container
        container = QGridLayout()

        # Add the widgets
        container.addWidget(self._header(), 0, 0, 1, 3)
        container.addWidget(self._status(), 1, 0, 1, 1)
        container.addWidget(self._track(), 1, 1, 1, 1)
        container.addWidget(self._button(), 1, 2, 1, 1)
        container.addWidget(self._left(), 2, 0, 2, 1)
        container.addWidget(self._right(), 2, 1, 1, 2)
        container.addLayout(self._buttons(), 3, 1, 1, 2)
        container.addWidget(self._footer(), 4, 0, 1, 3)

        widget = QWidget()
        widget.setLayout(container)
        self.setCentralWidget(widget)

        self.playlist.currentIndexChanged.connect(partial(self._update_index))
        
    def _header(self):
        shadow = QGraphicsDropShadowEffect()
        shadow.setBlurRadius(3)
        shadow.setOffset(3, 3)

        scene = QGraphicsScene()

        view = QGraphicsView()
        view.setMinimumSize(800, 100)
        view.setMaximumHeight(100)
        view.setScene(scene)

        gradient = QGradient(QGradient.RichMetal)

        scene.setBackgroundBrush(gradient)

        font = QFont('comic sans ms', 40, QFont.Bold)

        text = scene.addText('PyQt5 Music Player')
        text.setDefaultTextColor(QColor(250,250,250))
        text.setFont(font)

        text.setGraphicsEffect(shadow)

        return view

    def _update_index(self):
        self.music_list.setCurrentRow(self.playlist.currentIndex())
        if self.playlist.currentIndex() < 0:
            self.music_list.setCurrentRow(0)

    def _status(self):
        self.status = QLabel('Status: Stopped')
        self.status.setFrameShape(QFrame.Box)
        self.status.setFrameShadow(QFrame.Sunken)
        self.status.setStyleSheet('padding: 5px')
        self.status.setMaximumHeight(40)
        return self.status

    def _track(self):
        self.track = QLabel('Track: No track is playing')
        self.track.setFrameShape(QFrame.Box)
        self.track.setFrameShadow(QFrame.Sunken)
        self.track.setStyleSheet('padding: 5px')
        self.track.setMaximumHeight(40)
        return self.track

    def _button(self):
        button = QPushButton('Open Folder')
        button.setCursor(QCursor(Qt.PointingHandCursor))
        button.released.connect(self._get_files)
        button.setStyleSheet('''
        QPushButton{border: 1px solid gray; padding: 5px; border-style: outset;
        background-color: silver;}
        QPushButton:hover{ background-color: lightgray; padding: 5px;
        border-style: outset; border: 1px solid gray; font-weight: bold;}
        QPushButton:pressed {border: 2px solid gray; padding: 5px;
        background-color: silver; border-style: inset; font-weight: bold;}
        ''')
        return button

    def _get_files(self):
        files = QFileDialog.getOpenFileNames(None, 'Audio File Formats', filter='(*.mp3 *.wav *.ogg)')
        for file in files[0]:
            self.playlist.addMedia(QMediaContent(self.url.fromLocalFile(file)))
            file = file.split('/')
            self.music_list.addItem(str(file[-1][:-4]))
        self.music_list.setCurrentRow(0)
        self.playlist.setCurrentIndex(0)

    def _left(self):
        frame = QFrame()
        frame.setFrameShape(frame.Box)
        frame.setFrameShadow(frame.Sunken)
        frame.setMinimumHeight(300)
        return frame

    def _right(self):
        self.music_list = QListWidget()
        self.music_list.setFrameShape(QFrame.Box)
        self.music_list.setFrameShadow(QFrame.Sunken)
        self.music_list.setStyleSheet('background-color: snow;')
        return self.music_list

    def _exit(self):
        sys.exit()

    def _buttons(self):

        layout = QHBoxLayout()
        buttons = {
        'Play': partial(self._command, action='play'), 'Stop': partial(self._command, action='stop'),
        'Next': partial(self._command, action='next'), 'Prev': partial(self._command, action='prev'),
        'Clear List': partial(self._command, action='clear'), 'Exit':partial(self._exit)
        }

        for button, cmd in buttons.items():
            btn  = QPushButton(button)
            btn.setCursor(Qt.PointingHandCursor)
            btn.released.connect(cmd)
            if button == 'Exit':
                btn.setStyleSheet('''
                QPushButton{background-color: firebrick; color: black;}
                QPushButton:hover{background-color: tomato; color: red;
                font-weight: bold;}
                QPushButton:pressed{background-color: red; color: white; font-weight: normal;}
                ''')
            elif button == 'Clear List':
                btn.setStyleSheet('''
                                  QPushButton{background-color: darkorange;}
                                  QPushButton:hover{background-color: orange; color: red; font-weight: bold;}
                                  ''')
            else:
                btn.setStyleSheet('''
                QPushButton{background-color: skyblue;}
                QPushButton:hover{background-color: lightskyblue; font-weight: bold;}
                QPushButton:pressed{background-color: dodgerblue; font-weight: bold;}
                ''')
            layout.addWidget(btn)

        return layout

    def _command(self, action=None):
        if action == 'play':
            self.player.play()
        elif action == 'stop':
            self.player.stop()
        elif action == 'next':
            self.playlist.next()
            self.player.play()
        elif action == 'prev':
            self.playlist.previous()
            self.player.play()
        elif action == 'clear':
            self.music_list.clear()
            self.player.stop()
            self.playlist.clear()
        self.playlist.setCurrentIndex(0)
        self.music_list.setCurrentRow(self.playlist.currentIndex())

        if action == 'stop'  or action == 'clear':
            self.status.setText('Status: Stopped')
        else:
            if self.music_list.currentItem():
                self.track.setText(f'Track: {self.music_list.currentItem().text().title()}')
                self.status.setText('Status: Now Playing')
            else:
                pass

    def _footer(self):
        shadow = QGraphicsDropShadowEffect()
        shadow.setBlurRadius(3)
        shadow.setOffset(3, 3)

        scene = QGraphicsScene()

        view = QGraphicsView()
        view.setMinimumSize(800, 40)
        view.setMaximumHeight(40)
        view.setScene(scene)

        gradient = QGradient(QGradient.RichMetal)

        scene.setBackgroundBrush(gradient)

        font = QFont('comic sans ms', 10, QFont.Bold)

        text = scene.addText('my-python.org - 10/10/2021')
        text.setDefaultTextColor(QColor(250,250,250))
        text.setFont(font)

        text.setGraphicsEffect(shadow)

        return view

def main():
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())

main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
PyQt5 Music Player - by menator01 - Oct-14-2021, 09:38 AM
RE: PyQt5 Music Player - by Larz60+ - Oct-14-2021, 08:49 PM
RE: PyQt5 Music Player - by Axel_Erfurt - Oct-14-2021, 09:16 PM
RE: PyQt5 Music Player - by menator01 - Oct-15-2021, 05:58 AM
RE: PyQt5 Music Player - by menator01 - Oct-16-2021, 09:44 PM
RE: PyQt5 Music Player - by Axel_Erfurt - Oct-16-2021, 10:49 PM
RE: PyQt5 Music Player - by menator01 - Oct-16-2021, 11:00 PM
RE: PyQt5 Music Player - by Axel_Erfurt - Oct-17-2021, 09:26 PM
RE: PyQt5 Music Player - by menator01 - Oct-17-2021, 10:03 PM
RE: PyQt5 Music Player - by Axel_Erfurt - Oct-18-2021, 04:09 PM
RE: PyQt5 Music Player - by Axel_Erfurt - Oct-18-2021, 05:12 PM
RE: PyQt5 Music Player - by menator01 - Oct-18-2021, 06:44 PM
RE: PyQt5 Music Player - by Axel_Erfurt - Oct-19-2021, 05:21 PM
RE: PyQt5 Music Player - by menator01 - Oct-29-2021, 06:49 PM
RE: PyQt5 Music Player - by menator01 - Nov-02-2021, 09:34 AM
RE: PyQt5 Music Player - by Axel_Erfurt - Nov-02-2021, 08:04 PM
RE: PyQt5 Music Player - by menator01 - Nov-03-2021, 08:30 AM
RE: PyQt5 Music Player - by Axel_Erfurt - Nov-03-2021, 09:05 AM
RE: PyQt5 Music Player - by menator01 - Nov-03-2021, 09:06 AM
RE: PyQt5 Music Player - by Axel_Erfurt - Nov-03-2021, 09:11 AM
RE: PyQt5 Music Player - by menator01 - Nov-03-2021, 05:49 PM
RE: PyQt5 Music Player - by Axel_Erfurt - Nov-03-2021, 05:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Music Player with tkinter menator01 4 4,810 Jul-31-2021, 04:27 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020