Browse Source

player and screen

Karl 3 năm trước cách đây
mục cha
commit
45d5316b82
2 tập tin đã thay đổi với 102 bổ sung57 xóa
  1. 23 57
      player.py
  2. 79 0
      screen_output.py

+ 23 - 57
player.py

@@ -9,74 +9,40 @@ args = parser.parse_args()
 inputURI = args.URI_string
 
 
-async def playback_started_handler(data):
-    print(data)
-
-
-async def all_events_handler(event, data):
-    print(event, data)
-
-
-async def main3():
-    if inputURI == "stop":
-        await stop()
-    elif inputURI == "current_track":
-        await current_track()
+async def selector():
+    if "ytmusic" in inputURI or "http://" in inputURI:
+        await run_floppy(inputURI)
     else:
-        await main1()
-
-
-async def stop():
-    mopidy = await MopidyClient().connect()
-    await mopidy.playback.stop()
-    await mopidy.disconnect()
+        await action(inputURI)
 
 
-async def current_track():
+async def action(action):
     mopidy = await MopidyClient().connect()
-    res = await mopidy.playback.get_current_track()
-    current_data = {
-        "Artist": res["artists"][0]["name"],
-        "Album": res["album"]["name"],
-        "Song": res["name"],
-    }
-    print(current_data)
-    await mopidy.disconnect()
-
-
-async def main1():
-    link = inputURI.split(" - ")[1]
-    async with MopidyClient(
-        url="ws://127.0.0.1:6680/mopidy/ws"
-    ) as mopidy:  # close connection explicit
+    if action == "stop":
         await mopidy.playback.stop()
-        await mopidy.tracklist.clear()
-        await mopidy.tracklist.add(uris=[link])
+    elif action == "next":
+        await mopidy.playback.next()
+    elif action == "previous":
+        await mopidy.playback.previous()
+    elif action == "play":
         await mopidy.playback.play()
+    elif action == "pause":
+        await mopidy.playback.pause()
+    elif action == "resume":
+        await mopidy.playback.resume()
+    elif action == "shuffle":
+        await mopidy.tracklist.shuffle()
+    await mopidy.disconnect()
 
 
-async def main2():
+async def run_floppy(input):
+    link = input.split(" - ")[1]
     mopidy = await MopidyClient().connect()
     await mopidy.playback.stop()
     await mopidy.tracklist.clear()
-    await mopidy.tracklist.add(uris=[inputURI])
+    await mopidy.tracklist.add(uris=[link])
     await mopidy.playback.play()
-    mopidy.listener.bind("track_playback_started", playback_started_handler)
-    mopidy.listener.bind("*", all_events_handler)
-
-    # your app logic
-    for i in range(10):
-        # res = await mopidy.tracklist.get_tracks()
-        res = await mopidy.playback.get_current_track()
-        print(res)
-        await asyncio.sleep(5)
-
-    # end your app logic
-
-    await mopidy.disconnect()  # close connection implicit
+    await mopidy.disconnect()
 
 
-# asyncio.run(main1())
-# or
-# asyncio.run(main2())
-asyncio.run(main3())
+asyncio.run(selector())

+ 79 - 0
screen_output.py

@@ -0,0 +1,79 @@
+# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
+# SPDX-License-Identifier: MIT
+
+"""
+This demo will fill the screen with white, draw a black box on top
+and then print Hello World! in the center of the display
+
+This example is for use on (Linux) computers that are using CPython with
+Adafruit Blinka to support CircuitPython libraries. CircuitPython does
+not support PIL/pillow (python imaging library)!
+"""
+import asyncio
+import time
+import board
+import digitalio
+from PIL import Image, ImageDraw, ImageFont
+import adafruit_ssd1306
+
+from mopidy_async_client import MopidyClient
+from datetime import datetime
+
+
+async def current_track():
+    mopidy = await MopidyClient().connect()
+    res = await mopidy.playback.get_current_track()
+    b = datetime.fromtimestamp(
+        await mopidy.playback.get_time_position() / 1000.0
+    ).strftime("%M:%S")
+
+    if "ytmusic" in res["uri"]:
+        totalTime = res["length"]
+        a = datetime.fromtimestamp(totalTime / 1000.0).strftime("%M:%S")
+        current_data = {
+            "Artist": res["artists"][0]["name"],
+            "Album": res["album"]["name"],
+            "Song": res["name"],
+            "TrackTime": b + "/" + a,
+        }
+    else:
+        a = await mopidy.playback.get_stream_title()
+        data = a.split(" - ")
+        current_data = {
+            "Artist": res["name"],
+            "Album": data[0],
+            "Song": data[1],
+            "TrackTime": "",
+        }
+    await mopidy.disconnect()
+    oled.text(current_data["Artist"], 0, 16, 1)
+    oled.text(current_data["Album"], 0, 26, 1)
+    oled.text(current_data["Song"], 0, 36, 1)
+    oled.text(current_data["TrackTime"], 0, 46, 1)
+    return
+
+
+# Define the Reset Pin
+oled_reset = digitalio.DigitalInOut(board.D4)
+
+# Change these
+# to the right size for your display!
+WIDTH = 128
+HEIGHT = 64  # Change to 64 if needed
+BORDER = 0
+
+# Use for I2C.
+i2c = board.I2C()
+oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)
+
+
+async def main():
+    while True:
+        oled.fill(0)
+        oled.text("Floppy Player", 26, 0, 1)
+        await current_track()
+        oled.show()
+        time.sleep(5)
+
+
+asyncio.run(main())