Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new optional cacheKeyResolver #1005

Open
wants to merge 4 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions audio_service/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.18.10

* Add optional `cacheKeyResolver` for MediaItem art (@austinried).

## 0.18.9

* Fix cache bug in AudioServiceFragmentActivity (@Mordtimer).
Expand Down
19 changes: 15 additions & 4 deletions audio_service/lib/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,13 @@ class AudioService {
static BaseCacheManager get cacheManager => _cacheManager!;
static BaseCacheManager? _cacheManager;

/// The method used to generate the cache key for interacting with a
/// MedieItem's artwork in the cache manager.
/// Defaults to using [MediaItem.artUri.toString] as the cache key.
static FutureOr<String> Function(MediaItem mediaItem) get cacheKeyResolver =>
_cacheKeyResolver!;
static FutureOr<String> Function(MediaItem mediaItem)? _cacheKeyResolver;

static late AudioServiceConfig _config;
static late AudioHandler _handler;

Expand Down Expand Up @@ -919,13 +926,16 @@ class AudioService {
required T Function() builder,
AudioServiceConfig? config,
BaseCacheManager? cacheManager,
FutureOr<String> Function(MediaItem mediaItem)? cacheKeyResolver,
}) async {
assert(_cacheManager == null);
config ??= const AudioServiceConfig();
assert(config.fastForwardInterval > Duration.zero);
assert(config.rewindInterval > Duration.zero);
WidgetsFlutterBinding.ensureInitialized();
_cacheManager = (cacheManager ??= DefaultCacheManager());
_cacheKeyResolver = (cacheKeyResolver ??=
(MediaItem mediaItem) => mediaItem.artUri.toString());
final callbacks = _HandlerCallbacks();
_platform.setHandlerCallbacks(callbacks);
await _platform.configure(ConfigureRequest(config: config._toMessage()));
Expand Down Expand Up @@ -973,8 +983,8 @@ class AudioService {
_sendToPlatform(artUri.toFilePath());
} else {
// Try to load a cached file from memory.
final fileInfo =
await cacheManager.getFileFromMemory(artUri.toString());
final fileInfo = await cacheManager
.getFileFromMemory(await cacheKeyResolver(mediaItem));
final filePath = fileInfo?.file.path;
if (operationId != _artFetchOperationId) {
return;
Expand Down Expand Up @@ -1147,8 +1157,9 @@ class AudioService {
final headers = mediaItem.artHeaders;
final file = headers != null
? await cacheManager.getSingleFile(mediaItem.artUri!.toString(),
headers: headers)
: await cacheManager.getSingleFile(mediaItem.artUri!.toString());
key: await cacheKeyResolver(mediaItem), headers: headers)
: await cacheManager.getSingleFile(mediaItem.artUri!.toString(),
key: await cacheKeyResolver(mediaItem));
return file.path;
}
}
Expand Down
2 changes: 1 addition & 1 deletion audio_service/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: audio_service
description: Flutter plugin to play audio in the background while the screen is off.
version: 0.18.9
version: 0.18.10
homepage: https://github.com/ryanheise/audio_service/tree/master/audio_service

environment:
Expand Down