gdelataillade / alarm

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

Sometimes the sound is not restored. #174

Closed synstin closed 2 months ago

synstin commented 6 months ago

Alarm plugin version 3.0.14

Describe the bug When the alarm goes off, the music I have set as background music gets smaller and then doesn't come back.

For example, if I listen to it at volume 3 and when the break alarm goes off, it goes down to about 1 and then comes right back up, but sometimes it goes down to about 1 and then doesn't come back, even if I quit the app after that, it stays at 1 and I have to turn off the music app completely and turn it back on to get it back to the original volume.

Device info SM-N960N - Android 10 (SDK 29)

gdelataillade commented 6 months ago

Hi @synstin

OK I'll try to fix it in the next release.

gdelataillade commented 3 months ago

Hi @synstin

Do you still experience this kind of issue in the latest releases ?

pyjserv commented 3 months ago

Do you still experience this kind of issue in the latest releases ?

Hi @gdelataillade

I am experiencing the same issue. When I listen to YouTube live music, after the alarm music ends, the volume of the YouTube live music does not return to its original level. I have to close my app or reopen YouTube to restore it to normal. Additionally, if I use MX Player Pro or Solid Explorer to play music, the volume of the music playing does not decrease when the alarm goes off. My alarm version is 3.1.4, and the device is SM-A326BR/DS, Android 13.

Update: I tested older versions and found that versions 3.0.11 and earlier work fine, while starting from version 3.0.12, the volume of YouTube does not return to its original level.

pbouttier commented 2 months ago

Same things here, on 3.0.12 // 3.1.4 the sound is reduced until the app his killed, on Android. It's working in 3.0.11.

Thanks in advance

gdelataillade commented 2 months ago

Hi @pyjserv and @pbouttier

I'm looking at the differences between versions 3.0.11 and 3.0.12 but I don't see nothing related to the system volume... Do you have the issue all the time ? Or with what frequency ?

pbouttier commented 2 months ago

I tested it 2-3 times on each version, and it happened every time.

In 3.0.11 it seems to me that the app triggers a warning, java version 8 is used, maybe that's where it comes from? My test phone is running Android 11, so I haven't tested it with an emulator or any other version.

pyjserv commented 2 months ago

Hi @gdelataillade

I tested my two Samsung devices with Android 13 and Android 11, and the issue persists in versions 3.0.12 and above. However, it works fine in versions 3.0.11 and below.

gdelataillade commented 2 months ago

In your tests, do you guys set the loopAudio to false ? Or do you stop the alarm manually ? This way I could know better what piece of code is wrong.

@pbouttier Could you share the warning triggered by the app in 3.0.11 ?

pbouttier commented 2 months ago

warning seen only with --profile, not on debug, not on release

warning: [options] source value 8 is obsolete and will be removed in a future release
warning: [options] target value 8 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
3 warnings

I had the same problem on the core of my app, I switched to java 11 in build.gradle, like this

android {
   ....

    def javaVersion = 11;
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility = javaVersion // The minimum value
        targetCompatibility = javaVersion // The minimum value
    }

  ...
}

my settings are

  Future<void> setAlarm(DateTime dateTime) async {
    UserSettingsModel userSettings = await ref.read(userSettingsProvider.future);

    final alarmSettings = AlarmSettings(
      id: alarmId,
      dateTime: dateTime,
      assetAudioPath: 'assets/audio/alarm.mp3',
      loopAudio: false,
      vibrate: userSettings.noticeEndCountdown == NoticeEndCountdown.vibrate,
      // Le son est indépendant du son configuré à l'instant T sur Android, ça le remet brieffement à cette valeur
      volume: userSettings.noticeEndCountdown == NoticeEndCountdown.sound ? userSettings.countdownVolume : 0.0, 
      notificationTitle: tr('workout.ongoing.notification_countdown.title'),
      notificationBody: tr('workout.ongoing.notification_countdown.body'),
    );
    await Alarm.set(alarmSettings: alarmSettings);
  }

I tested with a fixed value for the sound, instead of my user settings, and it made no difference.

the audio is a 1s double bip.

pyjserv commented 2 months ago

Hi @gdelataillade

In your tests, do you guys set the loopAudio to false ?

I set loopAudio to true.

Or do you stop the alarm manually ?

Yes, I use Alarm.stop(id) to stop alarm.

I think I found the part causing the issue. In AlarmPlugin.kt, fun onMethodCall(), label "stopAlarm",

line 58 in version 3.0.12

change context.stopService(stopIntent) to context.startService(stopIntent)

This restores the volume to normal after calling Alarm.stop(id).

Update: Perhaps this issue is due to the service not being completely stopped.

gdelataillade commented 2 months ago

Hi guys,

I think I found a quick fix, at least it worked for my Android 11 device. I just released a new version (3.1.5), please tell me if it works for you.

What I did was just to make sure that the audio focus was abandoned when the AlarmService was destroyed.

pyjserv commented 2 months ago

Hi @gdelataillade

I have tested version 3.1.5, and it worked perfectly! Thank you very much for the fix!

gdelataillade commented 2 months ago

Nice ! I'm glad it works now and thank you so much for your help.

pbouttier commented 2 months ago

Working fine, thanks :)