44 — Playlist: a random music player
A music player walks a list of songs — load it from JSON, mix it with the random helper, and loop a menu until you quit.
Steps
Save five songs in
songs.json— a JSON list of dicts withtitleandartist:
run it →[ {"title": "Hello", "artist": "Adele"}, {"title": "Dynamite", "artist": "BTS"}, {"title": "Love Dive", "artist": "IVE"}, {"title": "Life Goes On", "artist": "BTS"}, {"title": "Butter", "artist": "BTS"} ]Load the list with
json_load, mix it withshuffle, and jump around withrandom_pick. Save the whole player asplaylist.nme:
run it →# playlist.nme — a random music player. # Run: nme r playlist use random latest use file latest songs = json_load("songs.json") shuffle(songs) current = 0 show f"Playlist loaded: {len(songs)} songs" while True: show "" show "Commands: next, prev, list, quit" ask command, "? " if command == "next": current = current + 1 if current >= len(songs): current = 0 song = songs[current] show f"Now playing: {song['title']} by {song['artist']}" elif command == "prev": current = current - 1 if current < 0: current = len(songs) - 1 song = songs[current] show f"Now playing: {song['title']} by {song['artist']}" elif command == "list": show f"Playlist ({len(songs)} songs):" for i in range(len(songs)): mark = "> " if i == current else " " show f"{mark}{i + 1}. {songs[i]['title']} by {songs[i]['artist']}" elif command == "quit": show "Bye!" breakRun it and feed the commands through a pipe:
printf 'next\nnext\nlist\nquit\n' | nme r playlistPlaylist loaded: 5 songs Commands: next, prev, list, quit ? Now playing: Butter by BTS Commands: next, prev, list, quit ? Now playing: Hello by Adele Commands: next, prev, list, quit ? Playlist (5 songs): 1. Life Goes On by BTS 2. Butter by BTS > 3. Hello by Adele 4. Love Dive by IVE 5. Dynamite by BTS Commands: next, prev, list, quit ? Bye!nextwraps past the end andprevwraps to the last song;shufflemixes the order.
Try it yourself
Add a count command that asks for an artist and prints how many of their songs are in the playlist — loop over songs and grow a counter.
What you learned
use random latestanduse file latestload both helpers together.json_load("songs.json")reads the song list;shuffle(songs)mixes it;random_pick(songs)jumps around.songs[current]reads one song; wrapping the index loops the playlist.- A
while Truemenu withask,show, andbreakdrives the player.