From 4e9131a08fd629f7545fa5cc9ae8f792aa28fd14 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Sun, 26 Jun 2022 20:40:21 +1000 Subject: [PATCH 1/2] Add artHeaders, fix artUri decode bug. --- .../ryanheise/audioservice/AudioService.java | 2 +- audio_service/lib/audio_service.dart | 27 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java b/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java index 5c0f09e6..e1c0d25c 100644 --- a/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java +++ b/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java @@ -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); diff --git a/audio_service/lib/audio_service.dart b/audio_service/lib/audio_service.dart index 658eb575..6171b008 100644 --- a/audio_service/lib/audio_service.dart +++ b/audio_service/lib/audio_service.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io'; import 'dart:isolate'; import 'dart:ui'; @@ -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? artHeaders; + /// Whether this is playable (i.e. not a folder). final bool? playable; @@ -633,6 +637,7 @@ class MediaItem { this.genre, this.duration, this.artUri, + this.artHeaders, this.playable = true, this.displayTitle, this.displaySubtitle, @@ -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; @@ -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; } From bd428a5484a6cd1c827aaa419cc11666629e3ce9 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Mon, 27 Jun 2022 23:20:03 +1000 Subject: [PATCH 2/2] Update CHANGELOG and pubspec.yaml. --- audio_service/CHANGELOG.md | 1 + audio_service/pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/audio_service/CHANGELOG.md b/audio_service/CHANGELOG.md index c3182882..077efbf5 100644 --- a/audio_service/CHANGELOG.md +++ b/audio_service/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.18.6 * Fix build when targeting Android 13. +* Add MediaItem.artHeaders. ## 0.18.5 diff --git a/audio_service/pubspec.yaml b/audio_service/pubspec.yaml index d155b7d2..9d7ef6b7 100644 --- a/audio_service/pubspec.yaml +++ b/audio_service/pubspec.yaml @@ -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: