[Python] Managing multiple simultaneous tasks with python for a simple music player

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have written a simple python script that act as a cli music player and organizer. I use the python input as a controller for the player such that for example when user enter "pause" the music will pause; when they enter "resume" the music will resume and so on. The main problem is when a music reach its end the player doesn't go for the next track because it waits for the user input. I need a solution which can run input separately and recognize the end of a track.

    I try anything from threading to concurrent futures and ... I expected that my player could recognize the end of a track and played the next song.

    def play_track(track):

    print("------------------------------------")
    print(f"Playing Track: | Title: {track['Title']} | Artist: {track['Artist']} | Album: {track['Album']}"
    f" | Date: {track['Date']} | Length: {track['Length']}")
    print("------------------------------------")

    playback = Playback()
    playback.load_file(track["Path"])
    playback.play()

    with concurrent.futures.ThreadPoolExecutor() as executor:
    future = executor.submit(control)

    while playback.active:

    mediactl = future.result()

    match mediactl:
    case "pause":
    playback.pause()
    print("Track is paused")
    case "resume":
    playback.resume()
    print("Track is resumed")
    case "stop":
    playback.stop()
    print("Playback is stopped")
    return
    case "next":
    playback.stop()
    print("Next track is playing")
    return "next"
    case "prev":
    playback.stop()
    print("Previous track is playing")
    return "prev"


    def control():

    mediactl = input("Control (pause | resume | stop | next | prev): ")
    print("------------------------------------")
    print()

    if mediactl in ("pause", "resume", "stop", "next", "prev"):
    return mediactl
    return "invalid"

    Continue reading...

Compartilhe esta Página