AudioService class

Client API to connect with and communciate with the background audio task.

You may use this API from your UI to send start/pause/play/stop/etc messages to your background audio task, and to listen to state changes broadcast by your background audio task. You may also use this API from other background isolates (e.g. android_alarm_manager) to communicate with the background audio task.

A client must connect to the service before it will be able to send messages to the background audio task, and must disconnect when communication is no longer required. In practice, a UI should maintain a connection exactly while it is visible. It is strongly recommended that you use AudioServiceWidget to manage this connection for you automatically.

Constructors

AudioService()

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
inherited

Static Properties

browseMediaChildren List<MediaItem>
The children of the current browse media parent.
read-only
browseMediaChildrenStream Stream<List<MediaItem>>
A stream that broadcasts the children of the current browse media parent.
read-only
connected bool
True if the UI is connected.
read-only
currentMediaItem MediaItem
The current MediaItem.
read-only
currentMediaItemStream Stream<MediaItem>
A stream that broadcasts the current MediaItem.
read-only
customEventStream Stream
A stream that broadcasts custom events sent from the background.
read-only
notificationClickEvent bool
The status of the notificationClick event.
read-only
notificationClickEventStream Stream<bool>
A stream that broadcasts the status of the notificationClick event.
read-only
playbackState PlaybackState
The current playback state.
read-only
playbackStateStream Stream<PlaybackState>
A stream that broadcasts the playback state.
read-only
positionStream Stream<Duration>
A stream tracking the current position, suitable for animating a seek bar. To ensure a smooth animation, this stream emits values more frequently on short media items where the seek bar moves more quickly, and less frequenly on long media items where the seek bar moves more slowly. The interval between each update will be no quicker than once every 16ms and no slower than once every 200ms. [...]
read-only
queue List<MediaItem>
The current queue.
read-only
queueStream Stream<List<MediaItem>>
A stream that broadcasts the queue.
read-only
running bool
True if the background audio task is running.
read-only
runningStream → dynamic
A stream indicating when the running state changes.
read-only
usesIsolate bool
True if the background task runs in its own isolate, false if it doesn't.
read-only

Static Methods

addQueueItem(MediaItem mediaItem) Future<void>
Sends a request to your background audio task to add an item to the queue. This passes through to the onAddQueueItem method in your background audio task.
addQueueItemAt(MediaItem mediaItem, int index) Future<void>
Sends a request to your background audio task to add a item to the queue at a particular position. This passes through to the onAddQueueItemAt method in your background audio task.
addQueueItems(List<MediaItem> mediaItems) Future<void>
A convenience method calls addQueueItem for each media item in the given list. Note that this will be inefficient if you are adding a lot of media items at once. If possible, you should use updateQueue instead.
click([MediaButton button = MediaButton.media]) Future<void>
Programmatically simulates a click of a media button on the headset. [...]
connect() Future<void>
Connects to the service from your UI so that audio playback can be controlled. [...]
createPositionStream({int steps: 800, Duration minPeriod: const Duration(milliseconds: 200), Duration maxPeriod: const Duration(milliseconds: 200)}) Stream<Duration>
Creates a new stream periodically tracking the current position. The stream will aim to emit steps position updates at intervals of duration / steps. This interval will be clipped between minPeriod and maxPeriod. This stream will not emit values while audio playback is paused or stalled. [...]
customAction(String name, [dynamic arguments]) Future
Sends a custom request to your background audio task. This passes through to the onCustomAction in your background audio task. [...]
disconnect() Future<void>
Disconnects your UI from the service. [...]
fastForward() Future<void>
Sends a request to your background audio task to fast forward by the interval passed into the start method. This passes through to the onFastForward method in your background audio task.
pause() Future<void>
Sends a request to your background audio task to pause playback. This passes through to the onPause method in your background audio task.
play() Future<void>
Sends a request to your background audio task to play the current media item. This passes through to 'onPlay' in your background audio task.
playFromMediaId(String mediaId) Future<void>
Sends a request to your background audio task to play a particular media item referenced by its media id. This passes through to the 'onPlayFromMediaId' method in your background audio task.
playMediaItem(MediaItem mediaItem) Future<void>
Sends a request to your background audio task to play a particular media item. This passes through to the 'onPlayMediaItem' method in your background audio task.
prepare() Future<void>
Sends a request to your background audio task to prepare for audio playback. This passes through to the onPrepare method in your background audio task.
prepareFromMediaId(String mediaId) Future<void>
Sends a request to your background audio task to prepare for playing a particular media item. This passes through to the onPrepareFromMediaId method in your background audio task.
removeQueueItem(MediaItem mediaItem) Future<void>
Sends a request to your background audio task to remove an item from the queue. This passes through to the onRemoveQueueItem method in your background audio task.
rewind() Future<void>
Sends a request to your background audio task to rewind by the interval passed into the start method. This passes through to the onRewind method in the background audio task.
seekBackward(bool begin) Future<void>
Sends a request to your background audio task to begin or end seeking backward. This method passes through to the onSeekBackward method in your background audio task.
seekForward(bool begin) Future<void>
Sends a request to your background audio task to begin or end seek forward. This method passes through to the onSeekForward method in your background audio task.
seekTo(Duration position) Future<void>
Sends a request to your background audio task to seek to a particular position in the current media item. This passes through to the onSeekTo method in your background audio task.
setBrowseMediaParent([String parentMediaId = MEDIA_ROOT_ID]) Future<void>
Sets the parent of the children that browseMediaChildrenStream broadcasts. If unspecified, the root parent will be used.
setRating(Rating rating, [Map<String, dynamic> extras]) Future<void>
Sends a request to your background audio task to set a rating on the current media item. This passes through to the onSetRating method in your background audio task. The extras map must only contain primitive types!
setRepeatMode(AudioServiceRepeatMode repeatMode) Future<void>
Sends a request to your background audio task to set the repeat mode. This passes through to the onSetRepeatMode method in your background audio task.
setShuffleMode(AudioServiceShuffleMode shuffleMode) Future<void>
Sends a request to your background audio task to set the shuffle mode. This passes through to the onSetShuffleMode method in your background audio task.
setSpeed(double speed) Future<void>
Sends a request to your background audio task to set the audio playback speed. This passes through to the onSetSpeed method in your background audio task.
skipToNext() Future<void>
Sends a request to your background audio task to skip to the next item in the queue. This passes through to the onSkipToNext method in your background audio task.
skipToPrevious() Future<void>
Sends a request to your background audio task to skip to the previous item in the queue. This passes through to the onSkipToPrevious method in your background audio task.
skipToQueueItem(String mediaId) Future<void>
Sends a request to your background audio task to skip to a particular item in the queue. This passes through to the onSkipToQueueItem method in your background audio task.
start({Function backgroundTaskEntrypoint, Map<String, dynamic> params, String androidNotificationChannelName: "Notifications", String androidNotificationChannelDescription, int androidNotificationColor, String androidNotificationIcon: 'mipmap/ic_launcher', bool androidShowNotificationBadge: false, bool androidNotificationClickStartsActivity: true, bool androidNotificationOngoing: false, bool androidResumeOnClick: true, bool androidStopForegroundOnPause: false, bool androidEnableQueue: false, Size androidArtDownscaleSize, Duration fastForwardInterval: const Duration(seconds: 10), Duration rewindInterval: const Duration(seconds: 10)}) Future<bool>
Starts a background audio task which will continue running even when the UI is not visible or the screen is turned off. Only one background audio task may be running at a time. [...]
stop() Future<void>
Sends a request to your background audio task to stop playback and shut down the task. This passes through to the onStop method in your background audio task.
updateMediaItem(MediaItem mediaItem) Future<void>
Sends a request to your background audio task to update the details of a media item. This passes through to the 'onUpdateMediaItem' method in your background audio task.
updateQueue(List<MediaItem> queue) Future<void>
Sends a request to your background audio task to replace the queue with a new list of media items. This passes through to the onUpdateQueue method in your background audio task.

Constants

MEDIA_ROOT_ID → const String
The root media ID for browsing media provided by the background task.
"root"