audio_service icon indicating copy to clipboard operation
audio_service copied to clipboard

iOS notifications only displayed after second audio played

Open speaking-in-code opened this issue 4 years ago • 6 comments

Describe the bug

The example app does not reliably display "now playing" notifications. They don't show up the first time audio plays. If you exit the player and then play again, the notifications are displayed.

I'm happy to debug more to see if I can find a fix, but some pointers would be helpful. I've verified that MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo is being initialized, and it looks identical to me on both the first and second play. Let me know if you have ideas for things to check for additional debugging.

Minimal reproduction project

Example app reproduces the problem.

To Reproduce Steps to reproduce the behavior: Reproduction steps:

  1. install the example app on a simulator (I'm using iPhone SE simulator)
  2. click "audio player" (audio starts playing)
  3. check for notifications: none. this is a bug.
  4. click the stop button
  5. click 'audio player' again
  6. check for notifications: this time they are visible.

Error messages

No error messages I can see.

Expected behavior

Notifications should appear the first time audio plays.

Screenshots

n/a

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

  • Device: iPhone SE second generation (simulator)
  • OS: iOS 13.5

Flutter SDK version

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.1, on Mac OS X 10.15.5 19F96, locale en-US)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
[✓] Android Studio (version 3.6)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.3.5)
[✓] IntelliJ IDEA Community Edition (version 2017.2.2)
[✓] Connected device (1 available)

Additional context Add any other context about the problem here.

speaking-in-code avatar Jun 01 '20 04:06 speaking-in-code

Thanks for offering to investigate and debug this one.

I think a workaround for this was posted here some time ago but searching through the past issues I was unable to find it. From memory, the workaround had something to do with preemptively creating and destroying a flutter isolate, which suggests somehow this may relate to the flutter_isolate package.

I only have access to an iOS simulator and I haven't been able to reproduce it unfortunately, but hopefully the above information provides some hints.

ryanheise avatar Jun 02 '20 02:06 ryanheise

This one reproduces just fine in a simulator, thankfully.

Creating and destroying an isolate had no impact.

Starting and stopping the audio does fix it.

So I guess that narrows it down a little bit. I can play around to find the minimal steps necessary to make the problem go away, and that should help find what's going wrong.

speaking-in-code avatar Jun 02 '20 05:06 speaking-in-code

Ah, I just found the issue I was referring to earlier:

https://github.com/ryanheise/audio_service/issues/301#issuecomment-624152228

The suggested workaround is quite interesting, particularly that it only works if the first task calls setState to playing. I have changed the state model since then, but I think the effect playing has on the iOS plugin should be logically equivalent, so I would guess this fact still holds in the latest code. In any case, this could be an important hint as to what's going on. It might be a timing or sequencing issue.

Interestingly, Drabuna also noted that this only happens on the simulator and not on a real device, and also that on the simulator the notification eventually appears, just after a delay.

ryanheise avatar Jun 02 '20 12:06 ryanheise

I've seen this as well, but can confirm that it's only affected my simulator and the issue was gone when actually built and deployed to iOS device.

duncan-iaria avatar Jun 04 '20 13:06 duncan-iaria

There are two separate issues going on:

iPhone 7 and below: notifications don't work at all. Tested in simulator for iPhone 5-7, and on iPhone 5s real device. Not sure if this is WAI or not. I thought notifications were supported even on these older devices?

iPhone 8 and up: notifications only appear after the second run, so possibly related to issue #301. I've only tested a simulator here, no idea whether this happens on a real device.

Is there an example objective C app that gets this right? The examples I'm seeing on Apple's web site are Swift, makes it a bit hard to judge whether the objective C APIs are being used correctly.

speaking-in-code avatar Jun 07 '20 00:06 speaking-in-code

Is this still an issue with 0.18.0?

ryanheise avatar Jul 18 '21 15:07 ryanheise