Play next works

This commit is contained in:
Keith Edmunds 2021-03-26 18:46:08 +00:00
parent 8abf4caf4a
commit aeda0880dd
3 changed files with 114 additions and 4 deletions

View File

@ -13,6 +13,9 @@ from sqlalchemy.orm import sessionmaker
from config import Config from config import Config
from log import ERROR, DEBUG
# from log import INFO, ERROR, DEBUG
# "Constants" # "Constants"
# Instantiate logging # Instantiate logging
pytiger.logging.config.basic_config(stderr=False, level=logging.INFO) pytiger.logging.config.basic_config(stderr=False, level=logging.INFO)
@ -88,6 +91,7 @@ class Tracks(Base):
@classmethod @classmethod
def get_or_create(cls, path): def get_or_create(cls, path):
DEBUG(f"get_or_create(cls, {path})")
try: try:
track = session.query(cls).filter(cls.path == path).one() track = session.query(cls).filter(cls.path == path).one()
except NoResultFound: except NoResultFound:
@ -105,6 +109,16 @@ class Tracks(Base):
print(f"Can't find track id {id}") print(f"Can't find track id {id}")
return None return None
@staticmethod
def get_track(id):
try:
DEBUG(f"get_track({id})")
track = session.query(Tracks).filter(Tracks.id == id).one()
return track
except NoResultFound:
ERROR(f"get_track({id}): not found")
return None
@staticmethod @staticmethod
def search_titles(text): def search_titles(text):
return ( return (

View File

@ -1,10 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os
import vlc import vlc
import sys import sys
from log import DEBUG from log import DEBUG, ERROR
# from log import INFO, ERROR, DEBUG
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow
@ -44,11 +44,88 @@ class RepeatedTimer:
self.is_running = False self.is_running = False
class Music:
def __init__(self):
self.current_track = {
"player": None,
"meta": None
}
self.next_track = {
"player": None,
"meta": None
}
self.previous_track = {
"player": None,
"meta": None
}
def get_current_artist(self):
return self.current_track['meta'].artist
def get_current_duration(self):
return self.current_track['meta'].duration
def get_current_fade_at(self):
return self.current_track['meta'].fade_at
def get_current_playtime(self):
return self.current_track['player'].get_time()
def get_current_silence_at(self):
return self.current_track['meta'].silence_at
def get_current_title(self):
return self.current_track['meta'].title
def get_last_artist(self):
return self.last_track['meta'].artist
def get_last_title(self):
return self.last_track['meta'].title
def get_next_artist(self):
return self.next_track['meta'].artist
def get_next_title(self):
return self.next_track['meta'].title
def play_next(self):
if self.previous_track['player']:
self.previous_track['player'].release()
if self.current_track['player']:
self.current_track['player'].stop()
self.previous_track = self.current_track
self.current_track = self.next_track
self.next_track = {
"player": None,
"meta": None
}
self.current_track['player'].play()
def resume_last(self):
pass
def set_next_track(self, id):
track = Tracks.get_track(id)
if not track:
ERROR(f"set_next_track({id}): can't find track")
return None
self.next_track['player'] = vlc.MediaPlayer(
os.path.join(Config.ROOT, track.path))
self.next_track['meta'] = track
return track.id
class Window(QMainWindow, Ui_MainWindow): class Window(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.setupUi(self) self.setupUi(self)
self.connectSignalsSlots() self.connectSignalsSlots()
self.music = Music()
record = Settings.get_int("mainwindow_x") record = Settings.get_int("mainwindow_x")
x = record.f_int or 1 x = record.f_int or 1
@ -92,12 +169,17 @@ class Window(QMainWindow, Ui_MainWindow):
def connectSignalsSlots(self): def connectSignalsSlots(self):
self.fileButton.clicked.connect(self.selectFile) self.fileButton.clicked.connect(self.selectFile)
self.databaseButton.clicked.connect(self.selectFromDatabase) self.databaseButton.clicked.connect(self.selectFromDatabase)
self.actionPlay_selected.triggered.connect(self.play_selected) self.actionPlay_selected.triggered.connect(self.play_next)
self.actionPlay_next.triggered.connect(self.play_next)
self.playlist.itemSelectionChanged.connect(self.set_next)
def selectFromDatabase(self): def selectFromDatabase(self):
dlg = DbDialog(self) dlg = DbDialog(self)
dlg.exec() dlg.exec()
def play_next(self):
self.music.play_next()
def play_selected(self): def play_selected(self):
if self.playlist.selectionModel().hasSelection(): if self.playlist.selectionModel().hasSelection():
row = self.playlist.currentRow() row = self.playlist.currentRow()
@ -124,6 +206,14 @@ class Window(QMainWindow, Ui_MainWindow):
# track = Track(fname) # track = Track(fname)
# self.add_to_playlist(track) # self.add_to_playlist(track)
def set_next(self):
if self.playlist.selectionModel().hasSelection():
row = self.playlist.currentRow()
track_id = int(self.playlist.item(row, 0).text())
DEBUG(f"set_next: track_id={track_id}")
if self.music.set_next_track(track_id) != track_id:
ERROR("Can't set next track")
def add_to_playlist(self, track): def add_to_playlist(self, track):
""" """
Add track to playlist Add track to playlist

View File

@ -322,6 +322,7 @@ border: 1px solid rgb(85, 87, 83);</string>
<string>Pla&amp;ylist</string> <string>Pla&amp;ylist</string>
</property> </property>
<addaction name="actionPlay_selected"/> <addaction name="actionPlay_selected"/>
<addaction name="actionPlay_next"/>
</widget> </widget>
<addaction name="menuFile"/> <addaction name="menuFile"/>
<addaction name="menuPlaylist"/> <addaction name="menuPlaylist"/>
@ -333,12 +334,17 @@ border: 1px solid rgb(85, 87, 83);</string>
</widget> </widget>
<action name="actionPlay_selected"> <action name="actionPlay_selected">
<property name="text"> <property name="text">
<string>Play selected</string> <string>&amp;Play selected</string>
</property> </property>
<property name="shortcut"> <property name="shortcut">
<string>Return</string> <string>Return</string>
</property> </property>
</action> </action>
<action name="actionPlay_next">
<property name="text">
<string>Play &amp;next</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>