ryanheise / audio_service

Flutter plugin to play audio in the background while the screen is off.
796 stars 474 forks source link

Local file ArtUri not showing in iOS #620

Open arshrahman opened 3 years ago

arshrahman commented 3 years ago

Which API doesn't behave as documented, and how does it misbehave? local artfile doesn't show in iOS. Web url works in iOS. Both local and web artUri is working in Android

Minimal reproduction project Save an image to the NSApplicationSupportDirectory of iOS. This can be done by calling getApplicationSupportDirectory() function of path_provider package.

example local artUri file in iOS: file:///var/mobile/Containers/Data/Application/4CF715DR-502C-4B98-A41C-8EA319821SE6/Library/Application%20Support/category/1.png Set MediaItem artUri to the local file Uri. Art Uri doesn't show in iOS media notification.

Here's a minimal code to copying an asset image to NSApplicationSupportDirectory of iOS and get the uri.

  1. Add path_provider: ^2.0.1 to pubspec.yaml
  2. Add asset image to 'pubspec.yaml'
  assets:
    - assets/category/1.png
Future<String> saveImagetoDevice() async {
    final path = join((await getApplicationSupportDirectory()).path, 'category', '1.png');
    if (FileSystemEntity.typeSync(path) == FileSystemEntityType.notFound) {
        final byteData = await rootBundle.load('assets/category/1.png');
        final bytes = byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes);
        await File(path).create(recursive: true)
          ..writeAsBytes(bytes);
    }
    return path;
}
final artUri = await saveImagetoDevice();
AudioService.updateQueue([    
MediaItem(
      id: "https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3",
      album: "Science Friday",
      title: "From Cat Rheology To Operatic Incompetence",
      artist: "Science Friday and WNYC Studios",
      duration: Duration(milliseconds: 2856950),
      artUri: artUri
)]);

To Reproduce (i.e. user steps, not code) Steps to reproduce the behavior:

  1. Set the MediaItem to local artUri
  2. Play the MediaItem
  3. iOS doesn't show the artUri

Expected behavior local artUri should also show in iOS media notification

Runtime Environment (please complete the following information if relevant):

Flutter SDK version

insert output of "flutter doctor" here
[✓] Flutter (Channel stable, 1.22.5, on macOS 11.0.1 20B29 darwin-x64, locale en-SG)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.5)
[✓] Android Studio (version 3.6)
[✓] VS Code (version 1.54.3)

Package Version

just_audio: ^0.6.13
audio_service: ^0.16.2
ryanheise commented 3 years ago

Minimal reproduction project Save an image to the NSApplicationSupportDirectory of iOS. This can be done by calling getApplicationSupportDirectory() function of path_provider package.

Can you do that and then provide me with a link that I can clone and run?

arshrahman commented 3 years ago

Here's a minimal project setup reproducing the error: https://github.com/arshrahman/audio_service

I just basically modified the example audio_service project to get a local artUri for the first MediaItem in the MediaLibrary class. All the changes are only in MediaLibrary class. There are two MediaItems. The first one has a local art uri and the second one has web image url. Both images are shown in Android media notification. However, only the web image is shown for iOS media notification.

ryanheise commented 3 years ago

Thanks, @arshrahman . Just to check, are you testing on a Simulator or an actual iPhone device?

ryanheise commented 3 years ago

Because there is a space in Application%20Support it is getting URL-encoded. But I'm not sure if the path on a real iPhone device will have the same problem.

My focus is now on the upcoming 0.18.0 release which should already have this bug fixed, so if it is urgent for you, you may consider trying this out in its preview form.

arshrahman commented 3 years ago

I am using an actual iPhone device. iPhone 11 model. Actually, in the iOS simulator, the media notification doesn't show at all. Not sure, if i need to turn on some setting to show the media notification

ryanheise commented 3 years ago

I see. On the simulator it was possible to test these things using an older version of Xcode but this can't be used on the BigSur. But as per your answer, the bug is present on both the simulator and real devices.

As before, I would suggest trying out the upcoming 0.18.0 release if it is urgent for you.

arshrahman commented 3 years ago

I see, I am running Bug Sur. I will try out 0.18.0 release a little later as it needs some migration. This is a minor bug and it doesn't affect the functionality.

zhouyuanbo commented 3 years ago

Test below code:

final artUri = await saveImagetoDevice(); AudioService.updateQueue([
MediaItem( artUri: "file://$artUri" )]);

ryanheise commented 3 years ago

@zhouyuanbo have you read my previous comment?

ryanheise commented 3 years ago

@arshrahman can you confirm whether the issue persists in 0.18.0?

ma-pe commented 2 years ago

I was trying artUri: Uri.file('assets/path-to-local-file.png') on just_audio_background: 0.0.1-beta.1. Unfortunately the album art does not show up.

edit: it seems that I am trying to achieve something else. I would like to use an image asset bundled with the app. For those assets one can not obtain a path.

In addition to that Uri.file does not do what I thought it would do, but creates a new file 🙃

sanjay23singh commented 2 years ago

@ma-pe have you found any solution to show artUri from assets folder?

ryanheise commented 2 years ago

@sanjay23singh that is not related to this issue. Please search for another existing issue for loading the artUri from assets, or if one does not exist, submit one.