QMediaPlayer version. Not working
This commit is contained in:
68
audio_player.py
Normal file
68
audio_player.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
from PyQt5.QtCore import QUrl, QFileInfo
|
||||
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
|
||||
|
||||
|
||||
class AudioPlayer(object):
|
||||
def __init__(self, play, pause, stop, volume, audio_progress):
|
||||
self.play = play
|
||||
self.pause = pause
|
||||
self.stop = stop
|
||||
self.volume = volume
|
||||
self.audio_progress = audio_progress
|
||||
self.audio_file = None
|
||||
self.player = QMediaPlayer()
|
||||
self.player.setVolume(51)
|
||||
self.player.durationChanged.connect(self.set_max_progress_bar)
|
||||
self.player.positionChanged.connect(self.set_progress_bar)
|
||||
self.play.clicked.connect(self.audio)
|
||||
self.pause.clicked.connect(self.pause_audio)
|
||||
self.stop.clicked.connect(self.stop_audio)
|
||||
self.player.stateChanged.connect(self.reset_audio_widget)
|
||||
self.volume.valueChanged.connect(self.set_volume)
|
||||
|
||||
def set_volume(self):
|
||||
self.player.setVolume(self.volume.value())
|
||||
|
||||
def reset_audio_widget(self):
|
||||
if self.player.state() == QMediaPlayer.StoppedState:
|
||||
self.audio_progress.setValue(0)
|
||||
self.volume.setValue(51)
|
||||
self.pause.setEnabled(False)
|
||||
self.stop.setEnabled(False)
|
||||
|
||||
def set_max_progress_bar(self):
|
||||
print(self.player.duration())
|
||||
self.audio_progress.setMaximum(self.player.duration())
|
||||
|
||||
def set_progress_bar(self):
|
||||
print(self.player.position())
|
||||
self.audio_progress.setValue(self.player.position())
|
||||
|
||||
def set_media_player(self):
|
||||
self.player.setMedia(QMediaContent(self.audio_file))
|
||||
|
||||
def set_audio_player(self, fname):
|
||||
self.reset_audio_widget()
|
||||
self.play.setEnabled(False)
|
||||
full_name = os.path.join('Data', 'Audio', fname + '.mp3')
|
||||
if QFileInfo(full_name).exists():
|
||||
print('exists')
|
||||
self.play.setEnabled(True)
|
||||
self.audio_file = QUrl.fromLocalFile(full_name)
|
||||
self.set_media_player()
|
||||
else:
|
||||
print('not exists')
|
||||
|
||||
def audio(self):
|
||||
self.player.play()
|
||||
self.stop.setEnabled(True)
|
||||
self.pause.setEnabled(True)
|
||||
|
||||
def stop_audio(self):
|
||||
self.player.stop()
|
||||
|
||||
def pause_audio(self):
|
||||
self.player.pause()
|
||||
BIN
icons_imgs/volume.png
Normal file
BIN
icons_imgs/volume.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
28
main.py
28
main.py
@@ -6,7 +6,10 @@ from PyQt5.QtWidgets import (QMainWindow,
|
||||
QMessageBox,)
|
||||
from PyQt5.QtGui import QPixmap
|
||||
from PyQt5 import uic
|
||||
from PyQt5.QtCore import QFileInfo
|
||||
from PyQt5.QtCore import QFileInfo, QSize
|
||||
import qtawesome as qta
|
||||
|
||||
from audio_player import AudioPlayer
|
||||
|
||||
qt_creator_file = "main_window.ui"
|
||||
|
||||
@@ -20,7 +23,24 @@ class MyApp(QMainWindow, Ui_MainWindow):
|
||||
self.load_db()
|
||||
self.display_signals()
|
||||
self.search_bar.textChanged.connect(self.display_signals)
|
||||
self.result_list.itemSelectionChanged.connect(self.display_spectrogram)
|
||||
self.result_list.itemSelectionChanged.connect(self.display_specs)
|
||||
self.play.setIcon(qta.icon('fa5.play-circle',
|
||||
color = "#7a7a7a",
|
||||
color_active = '#1d5eff'))
|
||||
self.play.setIconSize(self.play.size())
|
||||
self.pause.setIcon(qta.icon('fa5.pause-circle',
|
||||
color = "#7a7a7a",
|
||||
color_active = '#1d5eff'))
|
||||
self.pause.setIconSize(self.pause.size())
|
||||
self.stop.setIcon(qta.icon('fa5.stop-circle',
|
||||
color = "#7a7a7a",
|
||||
color_active = '#1d5eff'))
|
||||
self.stop.setIconSize(self.stop.size())
|
||||
self.audio_widget = AudioPlayer(self.play,
|
||||
self.pause,
|
||||
self.stop,
|
||||
self.volume,
|
||||
self.audio_progress)
|
||||
|
||||
def load_db(self):
|
||||
try:
|
||||
@@ -48,6 +68,10 @@ class MyApp(QMainWindow, Ui_MainWindow):
|
||||
if self.search_bar.text().lower() in signal.lower():
|
||||
self.result_list.addItem(signal)
|
||||
|
||||
def display_specs(self):
|
||||
self.display_spectrogram()
|
||||
self.audio_widget.set_audio_player(self.result_list.currentItem().text())
|
||||
|
||||
def display_spectrogram(self):
|
||||
spectrogram_name = self.result_list.currentItem().text()
|
||||
path_spectr = os.path.join("Data", "Spectra", spectrogram_name + ".jpg")
|
||||
|
||||
316
main_window.ui
316
main_window.ui
@@ -10,6 +10,12 @@
|
||||
<height>800</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
@@ -23,18 +29,16 @@ QPushButton {
|
||||
}
|
||||
|
||||
QProgressBar {
|
||||
/*border: 2px solid grey;*/
|
||||
border-radius: 8px;
|
||||
background-color: #7a7a7a
|
||||
border: 2px #7a7a7a;
|
||||
border-radius: 3px;
|
||||
background-color: #7a7a7a;
|
||||
}
|
||||
|
||||
QProgressBar::chunk {
|
||||
background-color: #1d5eff;
|
||||
width: 20px;
|
||||
border-radius: 7px
|
||||
}
|
||||
|
||||
</string>
|
||||
/*background-color: #1d5eff;*/
|
||||
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #1d5eff, stop:0.5 #4177ff, stop:1 #1d5eff);
|
||||
border-radius: 3px;
|
||||
}</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
@@ -186,11 +190,7 @@ QTabWidget::tab-bar {
|
||||
/* Style the tab using the tab sub-control. Note that
|
||||
it reads QTabBar _not_ QTabWidget */
|
||||
QTabBar::tab {
|
||||
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
|
||||
stop: 0 #7a7a7a, stop: 1.0 #7a7a7a);
|
||||
/*background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
|
||||
stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
|
||||
stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);*/
|
||||
background: #7a7a7a;
|
||||
border-top-left-radius: 8px;
|
||||
border-top-right-radius: 8px;
|
||||
min-width: 16ex;
|
||||
@@ -199,11 +199,7 @@ QTabBar::tab {
|
||||
}
|
||||
|
||||
QTabBar::tab:selected {
|
||||
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
|
||||
stop: 0 #999999, stop: 1.0 #999999);
|
||||
/* background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
|
||||
stop: 0 #fafafa, stop: 0.4 #f4f4f4,
|
||||
stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);*/
|
||||
background: #999999;
|
||||
color: #1d5eff
|
||||
}
|
||||
|
||||
@@ -240,15 +236,218 @@ QTabBar::tab:!selected {
|
||||
<attribute name="title">
|
||||
<string>Main</string>
|
||||
</attribute>
|
||||
<widget class="QLabel" name="spectrogram">
|
||||
<widget class="QWidget" name="layoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>480</x>
|
||||
<y>280</y>
|
||||
<width>221</width>
|
||||
<height>421</height>
|
||||
<x>270</x>
|
||||
<y>110</y>
|
||||
<width>161</width>
|
||||
<height>491</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QWidget" name="audio_widget" native="true">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">QPushButton {
|
||||
/*background-color: #1d5eff;*/
|
||||
border: 1px solid gray;
|
||||
border-color: #1d5eff;
|
||||
border-radius: 20px;
|
||||
}
|
||||
/*
|
||||
QPushButton:disabled {
|
||||
background-color: #7a7a7a;
|
||||
}
|
||||
*/</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="3" column="0" colspan="3">
|
||||
<widget class="QProgressBar" name="audio_progress">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>16777215</width>
|
||||
<height>10</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>75</number>
|
||||
</property>
|
||||
<property name="textVisible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="play">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>41</width>
|
||||
<height>41</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="checkable">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QPushButton" name="pause">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>41</width>
|
||||
<height>41</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="checkable">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QPushButton" name="stop">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>41</width>
|
||||
<height>41</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="3">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="pixmap">
|
||||
<pixmap>icons_imgs/volume.png</pixmap>
|
||||
</property>
|
||||
<property name="scaledContents">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QSlider" name="volume">
|
||||
<property name="styleSheet">
|
||||
<string notr="true">QSlider::groove:horizontal {
|
||||
/*border: 1px solid #999999;*/
|
||||
height: 6px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
|
||||
background: #7a7a7a;
|
||||
margin: 0 5px;
|
||||
border-radius: 3px
|
||||
}
|
||||
|
||||
QSlider::handle:horizontal {
|
||||
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 gray, stop:0.5 white, stop:1.0 gray);
|
||||
border: 1px solid #5c5c5c;
|
||||
width: 15px;
|
||||
margin: -5px -5px; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
|
||||
border-radius: 8px;
|
||||
}
|
||||
</string>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>100</number>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>51</number>
|
||||
</property>
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="spectrogram">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>153</width>
|
||||
<height>100</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
@@ -256,73 +455,8 @@ QTabBar::tab:!selected {
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QWidget" name="widget_2" native="true">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>50</x>
|
||||
<y>120</y>
|
||||
<width>371</width>
|
||||
<height>381</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>40</x>
|
||||
<y>40</y>
|
||||
<width>75</width>
|
||||
<height>23</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>PushButton</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QPushButton" name="pushButton_2">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>40</y>
|
||||
<width>75</width>
|
||||
<height>23</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>PushButton</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QPushButton" name="pushButton_3">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>240</x>
|
||||
<y>40</y>
|
||||
<width>75</width>
|
||||
<height>23</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>PushButton</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QProgressBar" name="progressBar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>110</x>
|
||||
<y>90</y>
|
||||
<width>118</width>
|
||||
<height>23</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>15</number>
|
||||
</property>
|
||||
<property name="textVisible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_2">
|
||||
|
||||
Reference in New Issue
Block a user