115 lines
2.7 KiB
Python
115 lines
2.7 KiB
Python
import st3m.run
|
|
import media
|
|
import uos
|
|
|
|
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):
|
|
def __init__(self, app_ctx):
|
|
super().__init__(app_ctx)
|
|
|
|
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
|
|
|
|
# ---------- 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):
|
|
super().on_enter(vm)
|
|
|
|
self.items = list_media_sorted(self.video_dir)
|
|
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):
|
|
media.stop()
|
|
|
|
# ---------- Main loop ----------
|
|
|
|
def think(self, ins, delta_ms):
|
|
super().think(ins, delta_ms)
|
|
|
|
# Media framework tick (REQUIRED)
|
|
media.think(delta_ms)
|
|
|
|
# ---- Petal controls ----
|
|
|
|
# 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():
|
|
media.pause()
|
|
else:
|
|
media.play()
|
|
|
|
# ---- Loop at end ----
|
|
dur = media.get_duration()
|
|
if dur > 0 and media.get_position() >= dur:
|
|
media.seek(0.0)
|
|
media.play()
|
|
|
|
# ---- Exit: long-press RIGHT app button ----
|
|
if self.input.buttons.app.right.down:
|
|
self._right_hold_ms += delta_ms
|
|
if self._right_hold_ms > 800:
|
|
self.vm.pop()
|
|
else:
|
|
self._right_hold_ms = 0
|
|
|
|
def draw(self, ctx):
|
|
media.draw(ctx)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
st3m.run.run_app(App)
|