audio_service icon indicating copy to clipboard operation
audio_service copied to clipboard

Add setFallbackArt to specify default art

Open nt4f04uNd opened this issue 2 years ago • 3 comments

Adds AudioService.setFallbackArt that automatically loads arts for all platforms using the existing refactored dart code.

Also fixes a bug with loadThumbnailUri where it was never used, which became obvious from another necessary change - reporting all errors from the native side to dart, to be able to fallback to default art.

Tested only on Android for now, this will need to be tested on other platforms

Fixes https://github.com/ryanheise/audio_service/issues/666 Fixes https://github.com/ryanheise/audio_service/issues/309

I wrote this PR because I needed this in my app. You can see how I'm using this API there if you want, there's also a video https://github.com/nt4f04uNd/sweyer/pull/80

https://user-images.githubusercontent.com/39104740/213883470-255b72e3-0378-42e7-82d6-bcb84e153fd4.mp4

Pre-launch Checklist

  • [x] I read the CONTRIBUTING.md and followed the process outlined there for submitting PRs.
  • [x] My change is not breaking and lands in minor branch OR my change is breaking and lands in major branch.
  • [x] If I'm the first to contribute to the next version, I incremented the version number in pubspec.yaml according to the pub versioning philosophy.
  • [x] I updated CHANGELOG.md to add a description of the change (format: * DESCRIPTION OF YOUR CHANGE (@your-git-username)).
  • [x] I updated/added relevant documentation (doc comments with ///).
  • [x] I ran dart analyze.
  • [x] I ran dart format.
  • [x] I ran flutter test and all tests are passing.

nt4f04uNd avatar Jan 21 '23 19:01 nt4f04uNd

Defining it as a static method on AudioService has turned out to be a bit messy as it tries to handle when initialisation methods are called out of order and the handler is(n't) ready yet. Some alternatives:

  1. The fallback parameters can be part of the init config object
  2. They can be separate parameters of the init method
  3. They can be properties of the handler so that different handlers can have different fallbacks
  4. They can be callback functions passed into the init method that take the url and handler as context parameters similar to how the placeholder and error images are handled in cached_network_image.

Hmm..

The primary reason the API is currently like this is because I need to be able to update the art dynamically. I considered options like 1, 2 and 4, but they don't allow this. Although, I never thought of the 3rd one, which also allows this and looks much better. Thanks for suggesting it :-)

I will try to make this working, for now I'll leave this PR as a draft.

nt4f04uNd avatar Jan 21 '23 19:01 nt4f04uNd

But Option 4 (like cached_network_image) can handle your use case as well, because it's a callback function. In the implementation of that function, you can dynamically adjust your desired behaviour.

ryanheise avatar Jan 22 '23 01:01 ryanheise

@nt4f04uNd AudioService.setFallbackArt method is working great for mobile. But it is not working for android auto's song library. I am showing default image from my assets and that image is showing in notification. But it is not working for media library in android auto.

ruchit-7span avatar Feb 28 '23 13:02 ruchit-7span