flutter_ringtone_player icon indicating copy to clipboard operation
flutter_ringtone_player copied to clipboard

Using android service to start and stop ringtones

Open aybefox opened this issue 4 years ago • 7 comments

In two scenarios you are not able to stop alarm ringtones again with the current implementation:

  1. If a app gets called back by the official android_alarm_manager
  2. If a app is reopened after it was killed, see #7

In the first case, when you call FlutterRingtonePlayer#playAlarm from the callback a second instance of FlutterRingtonePlayerPlugin is running. Because the reference to ringtone is different (null) from the first (start) call the stop call won't work. I'm not sure why there are two instances running but I guess it relates to the fact that the callback will not run in the same isolate as the main app.

In the second case the reference just gets lost.

Anyway, in both cases the reason is that FlutterRingtonePlayerPlugin holds the ringtone reference in a transient way.

This is why I moved the start/stop code to a separate android service.

aybefox avatar Apr 11 '20 00:04 aybefox

I just checked out: https://stackoverflow.com/questions/42126979/cannot-keep-android-service-alive-after-app-is-closed https://developer.android.com/guide/components/services#Foreground

The service in this pull request is a background service. Maybe it's not enough to fix the second case... I will stay tuned.

aybefox avatar Apr 11 '20 01:04 aybefox

Alarm sounds remain playing even if you close and reopen the app. You have to stop it explicitly by the given stop method.

aybefox avatar Apr 12 '20 16:04 aybefox

Have you tested this? Should I still use this Pull?

sanidhyaagrawal avatar Feb 03 '21 17:02 sanidhyaagrawal

This won't work if the asAlarm parameter set to true for the playNotification method case, please update that too.

Ted-chiptech avatar Mar 05 '21 00:03 Ted-chiptech

@aybefox Can you rebase your changes to current HEAD?

SPodjasek avatar Mar 25 '21 10:03 SPodjasek

Any update on this?

Alvarocda avatar Aug 31 '22 14:08 Alvarocda

This pull request has stalled.

github-actions[bot] avatar May 15 '24 00:05 github-actions[bot]