Загрузка...

Python Audiopler on Python

Thread in Your projects created by rasez Jul 20, 2025 at 10:44 PM. (bumped Jul 25, 2025 at 6:32 PM) 58 views

The poll

Как вам?

Your vote will be publicly visible.
  1. Хорошо

    1
    100%
  2. Плохо

    0
    0%
  3. На пайтоне аудиоплеер калище

    0
    0%
  1. rasez
    rasez Topic starter Jul 20, 2025 at 10:44 PM Стим аккаунты тут - lolz.live/threads/7680775 :da: 1481 Apr 29, 2025
    Йоу всем приветик :colobok_hi:
    Узнал о топ фишке фишко называемая как builder для всех qt библиотек
    И ЖОСКОО написал аудиоплеер красивенький на питончике :em:
    [IMG][IMG]
    ВОТ ТАК ВОТ ДА
    Python
    import sys
    import os
    from pathlib import Path
    from PySide6.QtWidgets import (
    QApplication, QMainWindow, QListWidget, QListWidgetItem, QFrame,
    QGraphicsDropShadowEffect
    )
    from PySide6.QtCore import Qt, QUrl, QFile, QPropertyAnimation, QEasingCurve
    from PySide6.QtMultimedia import QMediaPlayer, QAudioOutput
    from PySide6.QtUiTools import QUiLoader
    from PySide6.QtGui import QColor, QPalette, QLinearGradient, QGradient

    MUSIC_DIR = Path(__file__).parent / "music"

    class AudioPlayer(QMainWindow):
    def __init__(self):
    super().__init__()
    self.setup_ui()
    self.setup_player()
    self.setup_animations()
    self.load_music_folder(MUSIC_DIR)

    def setup_ui(self):
    loader = QUiLoader()
    file = QFile("player.ui")
    file.open(QFile.ReadOnly)
    self.ui = loader.load(file, self)
    file.close()


    self.setWindowFlags(Qt.FramelessWindowHint)
    self.setAttribute(Qt.WA_TranslucentBackground)


    self.apply_shadow(self.ui.btnPlayPause, 20)
    self.apply_shadow(self.ui.btnNext, 10)
    self.apply_shadow(self.ui.btnPrev, 10)
    self.apply_shadow(self.ui.listWidget, 15)
    self.apply_shadow(self.ui.labelTrackName, 10)


    self.ui.centralwidget.setStyleSheet("""
    background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
    stop:0 #1a1a2e, stop:1 #16213e);
    border-radius: 15px;
    """)


    self.ui.btnPlayPause.clicked.connect(self.play_pause)
    self.ui.btnNext.clicked.connect(self.next_track)
    self.ui.btnPrev.clicked.connect(self.prev_track)
    self.ui.sliderPosition.sliderMoved.connect(self.seek)
    self.ui.sliderVolume.valueChanged.connect(self.change_volume)
    self.ui.listWidget.itemClicked.connect(self.on_playlist_item_clicked)
    self.ui.btnClose.clicked.connect(self.close)
    self.ui.btnMinimize.clicked.connect(self.showMinimized)

    self.ui.show()

    def apply_shadow(self, widget, blur_radius):
    shadow = QGraphicsDropShadowEffect()
    shadow.setBlurRadius(blur_radius)
    shadow.setColor(QColor(100, 0, 150, 150))
    shadow.setOffset(0, 0)
    widget.setGraphicsEffect(shadow)

    def setup_player(self):
    self.player = QMediaPlayer()
    self.audio_output = QAudioOutput()
    self.player.setAudioOutput(self.audio_output)
    self.playlist = []
    self.current_index = -1
    self.is_playing = False

    self.player.positionChanged.connect(self.update_position)
    self.player.durationChanged.connect(self.update_duration)
    self.player.mediaStatusChanged.connect(self.media_status_changed)

    self.ui.sliderVolume.setRange(0, 100)
    self.ui.sliderVolume.setValue(30)
    self.audio_output.setVolume(0.3)
    self.ui.sliderPosition.setRange(0, 0)

    self.update_labels(0, 0)

    def setup_animations(self):

    self.setup_button_animation(self.ui.btnPlayPause)
    self.setup_button_animation(self.ui.btnNext)
    self.setup_button_animation(self.ui.btnPrev)


    self.fade_in = QPropertyAnimation(self, b"windowOpacity")
    self.fade_in.setDuration(300)
    self.fade_in.setStartValue(0)
    self.fade_in.setEndValue(1)
    self.fade_in.setEasingCurve(QEasingCurve.InOutQuad)
    self.fade_in.start()

    def setup_button_animation(self, button):
    effect = QGraphicsDropShadowEffect(button)
    effect.setColor(QColor(150, 50, 200))
    effect.setBlurRadius(0)
    effect.setOffset(0, 0)
    button.setGraphicsEffect(effect)

    self.anim = QPropertyAnimation(effect, b"blurRadius")
    self.anim.setDuration(200)
    button.enterEvent = lambda e: self.anim.start()
    button.leaveEvent = lambda e: self.anim.reverse()

    def load_music_folder(self, folder_path):
    if not folder_path.exists() or not folder_path.is_dir():
    print(f"Music folder not found: {folder_path}")
    return

    files = sorted([f for f in folder_path.iterdir() if f.suffix.lower() in [".mp3", ".wav", ".m4a"]])
    self.playlist = files
    self.current_index = 0 if files else -1
    self.update_playlist()

    if self.current_index >= 0:
    self.load_track(self.current_index, auto_play=False)

    def update_playlist(self):
    self.ui.listWidget.clear()
    for path in self.playlist:
    item = QListWidgetItem(path.stem)
    item.setToolTip(str(path))
    self.ui.listWidget.addItem(item)

    def on_playlist_item_clicked(self, item):
    index = self.ui.listWidget.row(item)
    self.load_track(index)

    def load_track(self, index, auto_play=True):
    if 0 <= index < len(self.playlist):
    self.current_index = index
    path = self.playlist[index]
    url = QUrl.fromLocalFile(str(path))


    anim = QPropertyAnimation(self.ui.labelTrackName, b"geometry")
    anim.setDuration(300)
    anim.setEasingCurve(QEasingCurve.OutCubic)
    anim.setStartValue(self.ui.labelTrackName.geometry())
    anim.setEndValue(self.ui.labelTrackName.geometry().adjusted(0, -10, 0, -10))
    anim.start()

    self.player.stop()
    self.player.setSource(QUrl())
    self.player.setSource(url)
    self.ui.labelTrackName.setText(path.stem)
    self.ui.sliderPosition.setValue(0)
    self.update_labels(0, 0)

    for i in range(self.ui.listWidget.count()):
    item = self.ui.listWidget.item(i)
    item.setSelected(i == index)
    if i == index:
    self.ui.listWidget.scrollToItem(item)

    if auto_play:
    self.play()
    else:
    self.pause()

    def play(self):
    self.player.play()
    self.is_playing = True

    self.ui.btnPlayPause.setText("PAUSE")
    self.ui.btnPlayPause.setStyleSheet("""
    QPushButton {
    font-size: 28pt;
    padding: 7px;
    border: 3px solid #9c27b0;
    border-radius: 35px;
    background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop:0 #6a11cb, stop:1 #2575fc);
    color: white;
    font-weight: 650;
    font-family: "Montserrat", sans-serif;
    }
    """)



    def pause(self):
    self.player.pause()
    self.is_playing = False

    self.ui.btnPlayPause.setText("PLAY")
    self.ui.btnPlayPause.setStyleSheet("""
    QPushButton {
    font-size: 28pt;
    padding: 7px;
    border: 3px solid #9c27b0;
    border-radius: 35px;
    background-color: #2d2d4d;
    color: #9c27b0;
    font-weight: 650;
    font-family: "Montserrat", sans-serif;
    }
    """)

    def play_pause(self):
    if self.is_playing:
    self.pause()
    else:
    self.play()

    def next_track(self):
    if self.playlist:
    next_index = (self.current_index + 1) % len(self.playlist)
    self.load_track(next_index)



    def prev_track(self):
    if self.playlist:
    prev_index = (self.current_index - 1) % len(self.playlist)
    self.load_track(prev_index)

    anim = QPropertyAnimation(self.ui.labelTrackName, b"pos")
    anim.setDuration(300)
    anim.setEasingCurve(QEasingCurve.OutCubic)

    anim.setEndValue(self.ui.labelTrackName.pos())
    anim.start()

    def seek(self, position):
    self.player.setPosition(position)

    def change_volume(self, value):
    volume = value / 100
    self.audio_output.setVolume(volume)


    self.ui.sliderVolume.setStyleSheet(f"""
    QSlider::groove:horizontal {{
    height: 6px;
    background: #535353;
    border-radius: 3px;
    }}
    QSlider::handle:horizontal {{
    background: #ffffff;
    border: none;
    width: 14px;
    height: 14px;
    margin: -4px 0;
    border-radius: 7px;
    }}
    QSlider::sub-page:horizontal {{
    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop:0 #6a11cb, stop:1 #2575fc);
    }}
    """)

    def update_position(self, position):
    self.ui.sliderPosition.setValue(position)
    self.update_labels(position, self.player.duration())

    def update_duration(self, duration):
    self.ui.sliderPosition.setRange(0, duration)
    self.update_labels(self.player.position(), duration)

    def update_labels(self, position, duration):
    self.ui.labelTotalTime.setText(f"{self.ms_to_time(position)} / {self.ms_to_time(duration)}")

    def ms_to_time(self, ms):
    seconds = ms // 1000
    minutes = seconds // 60
    seconds = seconds % 60
    return f"{minutes:02}:{seconds:02}"

    def media_status_changed(self, status):
    from PySide6.QtMultimedia import QMediaPlayer
    if status == QMediaPlayer.MediaStatus.EndOfMedia:
    self.next_track()

    def mousePressEvent(self, event):
    if event.button() == Qt.LeftButton:
    self.old_pos = event.globalPosition().toPoint()

    def mouseMoveEvent(self, event):
    if self.old_pos:
    delta = event.globalPosition().toPoint() - self.old_pos
    self.move(self.pos() + delta)
    self.old_pos = event.globalPosition().toPoint()

    def mouseReleaseEvent(self, event):
    self.old_pos = None

    def closeEvent(self, event):
    self.player.stop()
    self.player.setSource(QUrl())
    event.accept()

    if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle("Fusion")


    palette = QPalette()
    palette.setColor(QPalette.Window, QColor(26, 26, 46))
    palette.setColor(QPalette.WindowText, QColor(255, 255, 255))
    palette.setColor(QPalette.Base, QColor(45, 45, 77))
    palette.setColor(QPalette.AlternateBase, QColor(53, 53, 89))
    palette.setColor(QPalette.ToolTipBase, QColor(156, 39, 176))
    palette.setColor(QPalette.ToolTipText, QColor(255, 255, 255))
    palette.setColor(QPalette.Text, QColor(255, 255, 255))
    palette.setColor(QPalette.Button, QColor(45, 45, 77))
    palette.setColor(QPalette.ButtonText, QColor(255, 255, 255))
    palette.setColor(QPalette.BrightText, QColor(255, 0, 127))
    palette.setColor(QPalette.Highlight, QColor(106, 17, 203))
    palette.setColor(QPalette.HighlightedText, QColor(255, 255, 255))
    app.setPalette(palette)

    player = AudioPlayer()
    sys.exit(app.exec())
    Code
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>AudioPlayer</class>
    <widget class="QMainWindow" name="AudioPlayer">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>700</width>
    <height>800</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>Audio Player</string>
    </property>
    <property name="styleSheet">
    <string notr="true">background: transparent;</string>
    </property>
    <widget class="QWidget" name="centralwidget">
    <property name="styleSheet">
    <string notr="true">background: transparent;</string>
    </property>
    <layout class="QVBoxLayout" name="verticalLayoutMain">
    <property name="spacing">
    <number>15</number>
    </property>
    <property name="leftMargin">
    <number>20</number>
    </property>
    <property name="topMargin">
    <number>20</number>
    </property>
    <property name="rightMargin">
    <number>20</number>
    </property>
    <property name="bottomMargin">
    <number>20</number>
    </property>
    <item>
    <layout class="QHBoxLayout" name="horizontalLayoutHeader">
    <property name="spacing">
    <number>10</number>
    </property>
    <item>
    <widget class="QPushButton" name="btnClose">
    <property name="maximumSize">
    <size>
    <width>20</width>
    <height>20</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">QPushButton {
    background-color: #ff5555;
    border-radius: 10px;
    }
    QPushButton:hover {
    background-color: #ff0000;
    }</string>
    </property>
    <property name="text">
    <string/>
    </property>
    </widget>
    </item>
    <item>
    <widget class="QPushButton" name="btnMinimize">
    <property name="maximumSize">
    <size>
    <width>20</width>
    <height>20</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">QPushButton {
    background-color: #ffbb33;
    border-radius: 10px;
    }
    QPushButton:hover {
    background-color: #ff8800;
    }</string>
    </property>
    <property name="text">
    <string/>
    </property>
    </widget>
    </item>
    <item>
    <spacer name="horizontalSpacer">
    <property name="orientation">
    <enum>Qt::Orientation::Horizontal</enum>
    </property>
    <property name="sizeHint" stdset="0">
    <size>
    <width>40</width>
    <height>20</height>
    </size>
    </property>
    </spacer>
    </item>
    </layout>
    </item>
    <item>
    <widget class="QLabel" name="labelTrackName">
    <property name="minimumSize">
    <size>
    <width>0</width>
    <height>60</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">font-size: 18pt;
    font-weight: 600;
    color: white;
    background-color: rgba(45, 45, 77, 180);
    border-radius: 15px;
    padding: 10px;
    font-family: "Montserrat", sans-serif;
    </string>
    </property>
    <property name="text">
    <string>Трек не загружен</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignmentFlag::AlignCenter</set>
    </property>
    </widget>
    </item>
    <item>
    <widget class="QSlider" name="sliderPosition">
    <property name="minimumSize">
    <size>
    <width>0</width>
    <height>10</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">QSlider::groove:horizontal {
    height: 6px;
    background: #535353;
    border-radius: 3px;
    }
    QSlider::handle:horizontal {
    background: #ffffff;
    border: none;
    width: 14px;
    height: 14px;
    margin: -4px 0;
    border-radius: 7px;
    }
    QSlider::sub-page:horizontal {
    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop:0 #6a11cb, stop:1 #2575fc);
    }</string>
    </property>
    <property name="orientation">
    <enum>Qt::Orientation::Horizontal</enum>
    </property>
    </widget>
    </item>
    <item>
    <widget class="QLabel" name="labelTotalTime">
    <property name="styleSheet">
    <string notr="true">font-size: 12px;
    color: #b3b3b3;
    font-family: &quot;Montserrat&quot;, sans-serif;</string>
    </property>
    <property name="text">
    <string>00:00 / 00:00</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignmentFlag::AlignCenter</set>
    </property>
    </widget>
    </item>
    <item>
    <layout class="QHBoxLayout" name="horizontalLayoutControls">
    <property name="spacing">
    <number>30</number>
    </property>
    <item>
    <widget class="QPushButton" name="btnPrev">
    <property name="minimumSize">
    <size>
    <width>50</width>
    <height>50</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">font-size: 20pt;
    border: 2px solid #9c27b0;
    border-radius: 25px;
    background-color: #2d2d4d;
    color: #b388ff;
    font-family: &quot;Montserrat&quot;, sans-serif;
    font-weight: 600</string>
    </property>
    <property name="text">
    <string>PREV</string>
    </property>
    </widget>
    </item>
    <item>
    <widget class="QPushButton" name="btnPlayPause">
    <property name="minimumSize">
    <size>
    <width>70</width>
    <height>70</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">font-size: 28pt;
    border: 3px solid #9c27b0;
    border-radius: 35px;
    background-color: #2d2d4d;
    color: #9c27b0;
    font-weight: 650;
    font-family: &quot;Montserrat&quot;, sans-serif;
    padding: 5px;</string>
    </property>
    <property name="text">
    <string>PLAY</string>
    </property>
    </widget>
    </item>
    <item>
    <widget class="QPushButton" name="btnNext">
    <property name="minimumSize">
    <size>
    <width>50</width>
    <height>50</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">font-size: 20pt;
    border: 2px solid #9c27b0;
    border-radius: 25px;
    background-color: #2d2d4d;
    color: #b388ff;
    font-family: &quot;Montserrat&quot;, sans-serif;
    font-weight: 600</string>
    </property>
    <property name="text">
    <string>NEXT</string>
    </property>
    </widget>
    </item>
    </layout>
    </item>
    <item>
    <widget class="QListWidget" name="listWidget">
    <property name="minimumSize">
    <size>
    <width>0</width>
    <height>200</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">QListWidget {
    background-color: rgba(45, 45, 77, 150);
    border: 1px solid #535353;
    border-radius: 10px;
    color: #ffffff;
    font-size: 14px;
    padding: 5px;
    font-weight: 550;
    }
    QListWidget::item {
    padding: 8px;
    border-bottom: 1px solid #3d3d5c;
    }
    QListWidget::item:hover {
    background-color: rgba(106, 17, 203, 100);
    }
    QListWidget::item:selected {
    background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop:0 #6a11cb, stop:1 #2575fc);
    color: #ffffff;
    border-radius: 5px;
    }
    QScrollBar:vertical {
    border: none;
    background: transparent;
    width: 8px;
    margin: 0px;
    }
    QScrollBar::handle:vertical {
    background: #6a11cb;
    min-height: 20px;
    border-radius: 4px;
    }
    QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
    background: none;
    }</string>
    </property>
    <property name="frameShape">
    <enum>QFrame::Shape::NoFrame</enum>
    </property>
    </widget>
    </item>
    <item>
    <widget class="QSlider" name="sliderVolume">
    <property name="minimumSize">
    <size>
    <width>180</width>
    <height>25</height>
    </size>
    </property>
    <property name="styleSheet">
    <string notr="true">QSlider::groove:horizontal {
    height: 6px;
    background: #535353;
    border-radius: 3px;
    }
    QSlider::handle:horizontal {
    background: #ffffff;
    border: none;
    width: 14px;
    height: 14px;
    margin: -4px 0;
    border-radius: 7px;
    }
    QSlider::sub-page:horizontal {
    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
    stop:0 #6a11cb, stop:1 #2575fc);
    }</string>
    </property>
    <property name="maximum">
    <number>100</number>
    </property>
    <property name="value">
    <number>30</number>
    </property>
    <property name="orientation">
    <enum>Qt::Orientation::Horizontal</enum>
    </property>
    </widget>
    </item>
    </layout>
    </widget>
    </widget>
    <resources/>
    <connections/>
    </ui>
    Во так вот круто спасибо надеюсь вам нрав все
    Топови плеер все работает из папки music локально на вашем пк пока что к сожалению все
    GITHUB
    есть ещо одна версия в стиле лолза типо но она чутка криво выглядит :finger_up:
    [IMG]

    ЭТ БЫЛА БЕТА

     
Loading...
Top