improved controls, more videos
This commit is contained in:
85
__init__.py
85
__init__.py
@@ -1,43 +1,104 @@
|
|||||||
import st3m.run, media
|
import st3m.run
|
||||||
|
import media
|
||||||
|
import uos
|
||||||
|
|
||||||
from st3m.application import Application
|
from st3m.application import Application
|
||||||
|
|
||||||
|
|
||||||
|
VIDEO_EXTS = (".mpg", ".mpeg", ".mp3", ".mod")
|
||||||
|
|
||||||
|
|
||||||
|
def list_media_sorted(path):
|
||||||
|
files = []
|
||||||
|
for name in uos.listdir(path):
|
||||||
|
if any(name.lower().endswith(ext) for ext in VIDEO_EXTS):
|
||||||
|
files.append(name)
|
||||||
|
files.sort()
|
||||||
|
return files
|
||||||
|
|
||||||
|
|
||||||
class App(Application):
|
class App(Application):
|
||||||
def __init__(self, app_ctx):
|
def __init__(self, app_ctx):
|
||||||
super().__init__(app_ctx)
|
super().__init__(app_ctx)
|
||||||
self.path = "/sd/apps/paloma_blanca/output.mpg"
|
|
||||||
|
|
||||||
# Exit per long-press RIGHT
|
self.video_dir = "videos"
|
||||||
|
|
||||||
|
self.items = []
|
||||||
|
self.index = 0
|
||||||
|
|
||||||
|
# Petal mapping
|
||||||
|
self.PETAL_NEXT = 3
|
||||||
|
self.PETAL_PREV = 7
|
||||||
|
self.PETAL_PAUSE = 5
|
||||||
|
|
||||||
|
# Exit via long-press RIGHT (App button)
|
||||||
self._right_hold_ms = 0
|
self._right_hold_ms = 0
|
||||||
|
|
||||||
|
# ---------- Media helpers ----------
|
||||||
|
|
||||||
|
def load_current(self, autoplay=True):
|
||||||
|
media.stop()
|
||||||
|
path = self.video_dir + "/" + self.items[self.index]
|
||||||
|
print("Loading:", path)
|
||||||
|
media.load(path)
|
||||||
|
if autoplay:
|
||||||
|
media.play()
|
||||||
|
|
||||||
|
def next_video(self):
|
||||||
|
self.index = (self.index + 1) % len(self.items)
|
||||||
|
self.load_current(True)
|
||||||
|
|
||||||
|
def prev_video(self):
|
||||||
|
self.index = (self.index - 1) % len(self.items)
|
||||||
|
self.load_current(True)
|
||||||
|
|
||||||
|
# ---------- Lifecycle ----------
|
||||||
|
|
||||||
def on_enter(self, vm):
|
def on_enter(self, vm):
|
||||||
super().on_enter(vm)
|
super().on_enter(vm)
|
||||||
media.stop()
|
|
||||||
media.load(self.path)
|
self.items = list_media_sorted(self.video_dir)
|
||||||
media.play()
|
if not self.items:
|
||||||
|
raise RuntimeError("No media files found in " + self.video_dir)
|
||||||
|
|
||||||
|
self.index = 0
|
||||||
|
self.load_current(True)
|
||||||
|
|
||||||
def on_exit(self):
|
def on_exit(self):
|
||||||
media.stop()
|
media.stop()
|
||||||
|
|
||||||
|
# ---------- Main loop ----------
|
||||||
|
|
||||||
def think(self, ins, delta_ms):
|
def think(self, ins, delta_ms):
|
||||||
super().think(ins, delta_ms)
|
super().think(ins, delta_ms)
|
||||||
|
|
||||||
# Media-Framework laufen lassen
|
# Media framework tick (REQUIRED)
|
||||||
media.think(delta_ms)
|
media.think(delta_ms)
|
||||||
|
|
||||||
# DOWN: Play/Pause toggle
|
# ---- Petal controls ----
|
||||||
if self.input.captouch.petals[5].whole.pressed:
|
|
||||||
|
# Next video
|
||||||
|
if ins.captouch.petals[self.PETAL_NEXT].pressed:
|
||||||
|
self.next_video()
|
||||||
|
|
||||||
|
# Previous video
|
||||||
|
if ins.captouch.petals[self.PETAL_PREV].pressed:
|
||||||
|
self.prev_video()
|
||||||
|
|
||||||
|
# Play / Pause
|
||||||
|
if ins.captouch.petals[self.PETAL_PAUSE].pressed:
|
||||||
if media.is_playing():
|
if media.is_playing():
|
||||||
media.pause()
|
media.pause()
|
||||||
else:
|
else:
|
||||||
media.play()
|
media.play()
|
||||||
|
|
||||||
# Loop am Ende
|
# ---- Loop at end ----
|
||||||
dur = media.get_duration()
|
dur = media.get_duration()
|
||||||
if dur > 0 and media.get_position() >= dur:
|
if dur > 0 and media.get_position() >= dur:
|
||||||
media.seek(0.0)
|
media.seek(0.0)
|
||||||
media.play()
|
media.play()
|
||||||
|
|
||||||
# EXIT: RIGHT long-press (800ms)
|
# ---- Exit: long-press RIGHT app button ----
|
||||||
if self.input.buttons.app.right.down:
|
if self.input.buttons.app.right.down:
|
||||||
self._right_hold_ms += delta_ms
|
self._right_hold_ms += delta_ms
|
||||||
if self._right_hold_ms > 800:
|
if self._right_hold_ms > 800:
|
||||||
@@ -48,6 +109,6 @@ class App(Application):
|
|||||||
def draw(self, ctx):
|
def draw(self, ctx):
|
||||||
media.draw(ctx)
|
media.draw(ctx)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
st3m.run.run_app(App)
|
st3m.run.run_app(App)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user