gdelataillade / alarm

A Flutter plugin to easily manage alarms on iOS and Android
https://pub.dev/packages/alarm
MIT License
132 stars 86 forks source link

Alarm sound is playing in delay of 5 seconds #66

Closed dtkdt100 closed 1 year ago

dtkdt100 commented 1 year ago

Alarm plugin version latest

Describe the bug Alarm sound is playing in delay of 5 seconds. I am setting an alarm for DateTime.now() and only after 3 seconds the alarm rings

To Reproduce Steps to reproduce the behavior:

final alarmSettings = AlarmSettings(
      id: _ringingId,
      dateTime: DateTime.now(),
      assetAudioPath: assetAudioPath,
      loopAudio: false,
      vibrate: LocalMemory.vibrationOnAlarm!.value,
      enableNotificationOnKill: false,
    );
 await Alarm.set(alarmSettings: alarmSettings);

Expected behavior The alarm needs to go off in the same time it setts. Maybe 1 second delay

What I would do is putting if (dateTime is close to the time now) in android alarm.dart, so play alarm and not call AndroidAlarmManager.oneShotAt

Screenshots image

Device info Pixel 6, android 13

gdelataillade commented 1 year ago

Hi @dtkdt100,

Thanks for your interest in the plugin.

I must use AndroidAlarmManager.oneShotAt because if I don't, the alarm won't ring if the app is in background. This method creates an isolate to ensure that the code in playAlarm is executed whether app is in foreground or background.

Furthermore, I'm not sure that's the reason of the delay. I think a reason that could explain the delay is if the audio asset is large, because the player has to load it before being able to play it.

Do you have this issue with the example app ? If not, is your audio asset large ?

dtkdt100 commented 1 year ago

Hi, @gdelataillade . Thank you so much for replaying so fast!

The whole point of my issue that you don't need to use background alarm, because the datetime is so close (I mean in a metter of milliscronds to one second). Checkout my fork https://github.com/Dolev-Franco/alarm It is working good (you just need to add a if statement - if date is close).

My asset is not to large, I know it because in my fork it working well.

gdelataillade commented 1 year ago

Hi @dtkdt100

OK I understand your request. I'll add it to the plugin as soon as I find the time. I'll let you know when it's done.

dtkdt100 commented 1 year ago

@gdelataillade I am going to make a PR for this

dtkdt100 commented 1 year ago

@gdelataillade

PR: https://github.com/gdelataillade/alarm/pull/68