LynxAudioPlayer
Plays audio, such as a podcast, music, an audiobook or a meditation, and puts it on the lock screen, in Control Center and in Android's media notification, whose buttons control it. import { LynxAudioPlayer } from '@blotch/angular-lynx';
Lynx has no <audio> element and no HTMLAudioElement, so this is how an app plays sound. Every app has the player: it needs no permission.
Usage
load() resolves once the track is ready to play: state is then paused at its start, and duration says how long it is. play() plays it, from where it paused, or from its start once it has ended.
The url can be:
- an
https:URL, - a file the app bundles: import it (
import rain from './rain.mp3';), which gives its URL, - a
file://URL, such as aLynxAudioRecorderclip'suri.
The phone plays any format it knows, such as MP3, AAC (.m4a) and WAV.
Play in the background
Every app plays while it's on the screen, and pauses when it leaves: a play() while it's in the background rejects with failed, and the lock screen's Play does nothing. To play on with the app in the background and the phone locked, add backgroundAudio to the build target's options in angular.json:
What it adds:
- iOS: the
audiobackground mode (UIBackgroundModes). - Android: a media playback foreground service, whose notification holds the controls, with the
FOREGROUND_SERVICE,FOREGROUND_SERVICE_MEDIA_PLAYBACKandWAKE_LOCKpermissions. Android grants them when the app installs, without asking.
Turn it on only for an app that's listened to with the screen off:
- App Review rejects an app with the
audiomode that plays nothing in the background (guideline 2.5.4). - Google Play asks what the foreground service is for, in the Play Console's App content page, for an app that targets Android 14.
See Turn on device features for every setting.
The lock screen
While a track is loaded, the lock screen, Control Center, Android's notification, and headphones', a watch's and a car's buttons control it. They show the track's title, artist, album and artwork.
Play, pause and the slider act on the player by themselves. Next and previous show only while the app takes them, with onRemoteCommand():
A handler for play, pause or seek replaces what the button does, as a web page's navigator.mediaSession.setActionHandler() does. seek's handler gets the position the person moved the slider to, in seconds.
The handler goes when the component or service that added it is destroyed, or when you call the function onRemoteCommand() returns. Outside an injection context, pass { injector }. With two handlers for the same command, the newer one takes it until it's removed.
stop() takes the track off the lock screen and out of the notification.
Interruptions
The player pauses by itself, and state turns paused, when:
- a phone call or another app's audio takes over (iOS plays on after a call if iOS says to),
- headphones are unplugged,
- the app leaves the screen, without
backgroundAudio.
Taking the audio pauses other apps' music, and lets it play on again after stop().
Methods
state is loading while load() fetches the track, and whenever playback waits for more of it.
AudioTrack
AudioPlayerError
load() and play() reject with an AudioPlayerError, and error holds one for a track that stopped. Its code is one of:
Platform support
The web preview plays only after the person has interacted with the page, as browsers require: a play() before that rejects with failed.
Testing
The testing library fakes the player. A track loads at once and plays by the clock, so audioPlayback() says where it is, and pressRemoteCommand() presses a lock-screen button:
Related
LynxAudioRecorder: record a clip to play back- Turn on device features:
backgroundAudioand the other opt-in build options LynxAppState: whether the app is on the screen
