Lullaby Control
Lullaby control lets you play soothing audio tracks on a camera that supports it. Support is advertised by the device model, so check for it before showing any lullaby controls.
Pre-requisite: The user has to be signed in to perform the following operations.
Check whether a device supports lullabies
Lullaby support and the options a device accepts are exposed on the device model properties. When lullabyProperties is null the device does not support lullabies.
val lullaby = deviceModel.modelDetail.properties?.lullabyProperties
// lullaby?.supportedModes are the playback modes the device accepts
// lullaby?.supportedTimers are the timer durations in minutes the device accepts
// lullaby?.supportedTracks are the tracks that can be played
Each entry in supportedTracks is a LullabyTrackInfo:
LullabyTrackInfo(
id = "trackId", // The track id to pass when playing
name = "Ocean Waves", // The display name of the track
description = "Gentle ocean waves" // A short description of the track
)
Get the lullaby settings
To get the current lullaby settings of a device, The following method can be used:
spaceId(required): The space ID of the device.deviceId(required): The device ID.
InstaVision.deviceServices.getLullabySettings(
spaceId = "spaceId",
deviceId = "deviceId",
onSuccess = { lullabySetting ->
// The object contains the current playback mode, state, track and timer
},
onError = { error ->
// The error object contains the error code and message
},
)
The returned LullabySetting looks like this:
LullabySetting(
playbackMode = PlaybackMode.Loop.name, // How the tracks are cycled
timerDurationInMins = 30L, // How long playback runs before stopping
playbackState = PlaybackState.Playing.name, // The current playback state
trackId = "trackId", // The track being played, null when none is selected
playbackStartedAt = 1756636800000, // When playback started, in epoch milliseconds
stateUpdatedAt = 1756636800000 // When the state last changed, in epoch milliseconds
)
playbackMode matches a PlaybackMode value (PlayAll or Loop) and playbackState matches a PlaybackState value (Playing, Paused or Stopped). Use PlaybackState.fromValue(...) to turn the string into an enum.
Update the lullaby settings
To change the playback mode or the timer of a device, The following method can be used:
device(required): The device whose settings are to be updated.request(required): The settings to apply. Both of its fields are optional, so you can update either one on its own.
val request = UpdateLullabySettingRequest(
playbackMode = PlaybackMode.Loop.name, // The playback mode to apply
timerDurationInMins = 30L // The timer duration in minutes
)
InstaVision.deviceServices.updateLullabySettings(
device = device,
request = request,
onSuccess = { lullabySetting ->
// The updated lullaby setting
},
onError = { error ->
// The error object contains the error code and message
},
)
Pass only the values from supportedModes and supportedTimers that the device advertises.
Play a lullaby
To start playback on a device, The following method can be used:
device(required): The device on which to play.request(optional): The track to play. Omit it to play with the device’s current selection.
InstaVision.deviceServices.playLullaby(
device = device,
request = PlayLullabyRequest(trackId = "trackId"), // Optional, omit to use the current track
onSuccess = {
// Playback has started
},
onError = { error ->
// The error object contains the error code and message
},
)
Pause a lullaby
To pause playback on a device, The following method can be used:
device(required): The device on which to pause.
InstaVision.deviceServices.pauseLullaby(
device = device,
onSuccess = {
// Playback has been paused
},
onError = { error ->
// The error object contains the error code and message
},
)
Resume a lullaby
To resume paused playback on a device, The following method can be used:
device(required): The device on which to resume.
InstaVision.deviceServices.resumeLullaby(
device = device,
onSuccess = {
// Playback has resumed
},
onError = { error ->
// The error object contains the error code and message
},
)
Stop a lullaby
To stop playback on a device, The following method can be used:
device(required): The device on which to stop.
InstaVision.deviceServices.stopLullaby(
device = device,
onSuccess = {
// Playback has stopped
},
onError = { error ->
// The error object contains the error code and message
},
)
Keeping the playback state in sync
The playback state can also change from another app or from the device itself. Rather than polling getLullabySettings, listen for the LullabyStateUpdated realtime event and refresh when it arrives. See Realtime Events.
InstaVision.realtimeEventService.onMessage = { event ->
if (RealtimeEventType.fromValue(event.name) == RealtimeEventType.LullabyStateUpdated) {
// The lullaby state changed, refresh the settings for the device
}
}