4

I'm coding an alarm app using Flutter (Android only, for now).

I managed to play the alarm using a notification. But it only works when the phone isn't in Silent Mode.

Is there a way to build such an app using Flutter? Playing the sound no matter the state of the phone is the core functionality of the app. If this isn't possible, I should stop the development of this app.

Already tried using a sound played by AwesomeNotifications plugin (using Alarm Type with max priority) and a sound played by FlutterRingtonePlayer (triggered by a notification).

Flutter 3.3.5 • channel stable

Physical device used to test is running Android 13

Edit: as the app is an alarm, an isolate will play the sound.

  • I'm not an Android person but on iOS it is not possible for a third party app to break the silence of the silent switch. Good timer apps such as Multitimer have to beg the user to turn silent mode off. – matt Jan 09 '23 at 01:54
  • For Android, there is even a plugin to set the "sound mode" https://pub.dev/packages/sound_mode. I haven't tested it yet, because I don't think this is a good solution for my case – Rafael Nicolay Jan 09 '23 at 10:54
  • You have a bunch of output streams to choose from. **call, system, media, ringer, alarm, notification** all of them aren't muted in regular **silent mode** by default, but they can be if device owners change settings. The **do not disturb** mode is way stricter than **silent mode** though. – svin83 Jan 21 '23 at 10:06

3 Answers3

1

1-Add sound_mode as a dependency in your pubspec.yaml file

dependencies: flutter: sdk: flutter flutter_localizations: sdk: flutter

sound_mode: ^2.0.2

2-Add the following permission to AndroidManifest.xml for the app to appear in the 'Do Not Disturb Access' list :

<manifest ... >
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
    
    <application ... >
    ...
</manifest>

To change the device's sound mode:

import 'package:sound_mode/utils/ringer_mode_statuses.dart';

//To get the device's current sound mode:
String ringerStatus = await SoundMode.ringerModeStatus;
print(ringerStatus);

// To change the device's sound mode: silent to normal.
if(ringerStatus == 'silent'){
   try {
      await SoundMode.setSoundMode(RingerModeStatus.normal);
     } on PlatformException {
     print('Please enable permissions required');
    }
}

// your code here : play a sound
     
  • I think that this solution is too intrusive for the user. I would not like an app doing this in my phone. Not discarding the solution though, but this would be my last shot. – Rafael Nicolay Jan 23 '23 at 23:56
0
MediaPlayer mp= MediaPlayer.create(contexto, R.raw.your_sound);
mp.start();

You should call this when you start your alarm(you should call this in native code)

Anand
  • 4,355
  • 2
  • 35
  • 45
0

Once try with this because this package is providing functionality to work in silent mode also.

assets_audio_player: ^3.0.6

https://pub.dev/packages/assets_audio_player

https://i.stack.imgur.com/qbjFB.png

I hope this things are solve your issue.

Romil Mavani
  • 376
  • 6
  • This seemed promising. But at my first try, it couldn't even play audio when the phone wasnt in silent mode. When the app is open, it works even in silent mode. But when trying to run it from an isolate, it doesnt. Maybe Im doing something wrong, I'll dig more into it in the next days. – Rafael Nicolay Jan 23 '23 at 23:53
  • Oky but in my case it is working in Silent Mode also and worked while app in foreground, once check and try i hope it will work. – Romil Mavani Jan 25 '23 at 03:45
  • Really? Thats excelent to know! Have you tested in Android 13 also? – Rafael Nicolay Jan 26 '23 at 11:52
  • No, i don't have tested in android 13 device but i used in one of past my app and it is worked fine. – Romil Mavani Jan 26 '23 at 13:25