Spotify-style background audio¶
A music app is two screens and a bar: a library to choose from, a player that shows what is playing, and a small bar that follows you everywhere else. The music keeps going when the screen changes, when the app goes to the background and when the phone is locked.
The whole app¶
from apkpy_lib import Screen, audio, button, image, inputs, label, mini_player, run
library = Screen(id="library", scroll=True)
player = Screen(id="player")
label("Night Library", variant="title", screen=library)
sources = [
"https://cdn.example.com/northbound.mp3",
"https://cdn.example.com/afterimage.mp3",
]
titles = ["Northbound", "Afterimage"]
artists = ["Signal Club", "Mira Vale"]
artwork = [
"https://cdn.example.com/northbound.jpg",
"https://cdn.example.com/afterimage.jpg",
]
def play_mix():
audio.play_playlist(
sources,
titles=titles,
artists=artists,
arts=artwork,
start=0,
)
button("Play mix", command=play_mix, screen=library)
cover = image("", id="np_cover", describe="Album cover", screen=player)
title = label("", id="np_title", screen=player)
artist = label("", id="np_artist", screen=player)
progress = inputs("", type="range", id="np_progress", screen=player)
elapsed = label("0:00 / 0:00", id="np_time", screen=player)
button("", icon="skip_previous", describe="Previous", command=audio.previous, screen=player)
play_pause = button("", id="np_play", icon="play_arrow", describe="Play or pause", screen=player)
button("", icon="skip_next", describe="Next", command=audio.next, screen=player)
shuffle = button("", id="np_shuffle", icon="shuffle", describe="Shuffle", screen=player)
repeat = button("", id="np_repeat", icon="repeat", describe="Repeat", screen=player)
audio.now_playing(progress=progress, time=elapsed, cover=cover, title=title, artist=artist)
audio.controls(play_pause=play_pause, shuffle=shuffle, repeat=repeat)
mini_player(open=player)
run(library)
What each part does¶
audio.play_playlist()starts a queue. The four lists go together by position: the first source has the first title, artist and cover.start=is the index to begin at, or the source itself.audio.now_playing()binds the player screen to whatever is playing. The cover, title and artist change with the track, the time label countselapsed / duration, and dragging the range seeks.audio.controls()makes three buttons follow the player. Play and pause swap icons by themselves; shuffle and repeat light up in the button'sactive-color, and repeat goes off, all, one.mini_player(open=player)puts a bar with the cover, the title and a play/pause button at the bottom of every screen. It hides while nothing plays, and a tap on it opens the player screen.describe=is what TalkBack reads for a button that has only an icon.
Colour the lit buttons with one line of CSS:
What Android receives¶
Background playback belongs to a foreground service that the build generates,
with a native MediaSession. That is what puts the title, artist, cover and
controls in the notification and on the lock screen, and what lets headphones
and car controls pause and skip. When a call or another app needs the audio,
the player pauses or lowers its volume, and resumes after.
The screens do not own the music: they show and control the service's state. That is why changing screens never restarts a track.
The build declares the permissions this needs (a media foreground service,
notifications, and the internet for web sources). An audio file or a cover that
sits in your app's folder, such as "tracks/northbound.mp3", is packaged with
the app and found on the phone by the same name.
In the Previewer¶
The same calls play on your computer: the queue, the bound player screen, the mini-player and the buttons behave as they do on the phone, so the whole flow can be tried before a build.
What it does not do yet¶
Normal source playback, downloaded files, queues, playlists, favourites, seek, shuffle and repeat are supported. ApkPy does not currently promise DRM, automatic adaptive audio quality, crossfade, a smart cache or gapless playback for every codec and device.
Audio, playlists and Spotify has the rest of the API: favourites, saved playlists, downloads for offline listening and Spotify sign-in.