Skip to content

Commit

Permalink
Merge branch 'feature/art_headers' into minor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Jun 27, 2022
2 parents fd243a7 + bd428a5 commit 8458777
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions audio_service/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.18.6

* Fix build when targeting Android 13.
* Add MediaItem.artHeaders.

## 0.18.5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ synchronized void setMetadata(MediaMetadataCompat mediaMetadata) {
} else {
// Load content:// URIs
String artUri = mediaMetadata.getString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI);
if (artUri != null) {
if (artUri != null && artUri.startsWith("content:")) {
String loadThumbnailUri = mediaMetadata.getString("loadThumbnailUri");
artBitmap = loadArtBitmap(artUri, loadThumbnailUri);
mediaMetadata = putArtToMetadata(mediaMetadata);
Expand Down
27 changes: 19 additions & 8 deletions audio_service/lib/audio_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'dart:ui';

Expand Down Expand Up @@ -590,18 +591,21 @@ class MediaItem {
/// ## Speeding up Android content URI loading
///
/// For Android content:// URIs, the plugin by default uses
/// `ContentResolver.openFileDescriptor`, which takes the direct URI
/// of an image.
/// `ContentResolver.openFileDescriptor`, which takes the direct URI of an
/// image.
///
/// On Android API >= 29 there is `ContentResolver.loadThumbnail` function
/// which takes a URI of some content (for example, a song from `MediaStore`),
/// and returns a thumbnail for it.
///
/// It is noticeably faster to use this function. You can enable this by putting
/// a `loadThumbnailUri` key into the [extras]. If `loadThumbnail` is not available,
/// it will just fallback to using `openFileDescriptor`.
/// It is noticeably faster to use this function. You can enable this by
/// putting a `loadThumbnailUri` key into the [extras]. If `loadThumbnail` is
/// not available, it will just fallback to using `openFileDescriptor`.
final Uri? artUri;

/// The HTTP headers to use when sending an HTTP request for [artUri].
final Map<String, String>? artHeaders;

/// Whether this is playable (i.e. not a folder).
final bool? playable;

Expand Down Expand Up @@ -633,6 +637,7 @@ class MediaItem {
this.genre,
this.duration,
this.artUri,
this.artHeaders,
this.playable = true,
this.displayTitle,
this.displaySubtitle,
Expand Down Expand Up @@ -961,6 +966,7 @@ class AudioService {
filePath = await _loadArtwork(mediaItem);
// If we failed to download the art, abort.
if (filePath == null) continue;
if (File(filePath).lengthSync() == 0) continue;
// If we've already set a new media item, cancel this request.
// XXX: Test this
//if (mediaItem != _handler.mediaItem.value) continue;
Expand Down Expand Up @@ -1115,13 +1121,18 @@ class AudioService {
if (artUri.scheme == 'file') {
return artUri.toFilePath();
} else {
final file =
await cacheManager.getSingleFile(mediaItem.artUri!.toString());
final headers = mediaItem.artHeaders;
final file = headers != null
? await cacheManager.getSingleFile(mediaItem.artUri!.toString(),
headers: headers)
: await cacheManager.getSingleFile(mediaItem.artUri!.toString());
return file.path;
}
}
} catch (e) {
} catch (e, st) {
// TODO: handle this somehow?
// ignore: avoid_print
print('Error loading artUri: $e\n$st');
}
return null;
}
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.5
version: 0.18.6
homepage: https://github.com/ryanheise/audio_service/tree/master/audio_service

environment:
Expand Down

0 comments on commit 8458777

Please sign in to comment.