this is more so a question asking about how to approach a problem vs me posting code and asking where the error is.
But I am building a paging system in flutter (to work on both android and ios). As a result, I am using custom sounds in my application (so that I can have sounds stronger than the default OS sound). Because this sounds are naturally louder, I want to give the user the option to control the sound level of the notification (from 0 to 100%). I cannot seem to find any resources that speak on this, but I do know it is possible because I have apps on my phone that allow that (amazon pager app for instance).
Is there a way to allow a user control the volume level of a notification through the app that was built in flutter? If it's possible, could i get pointed to a tutorial or article or even sample code?
There's no dedicated way to do this but here's a hack to achieve this. You can use any package/widget of your choice like volume_control or volume_controller
Assuming that you use the first option:
VolumeControl.volume
Get user desired volume level values(Using a slider etc).
Save both values.
Now here you need to set the device notifications/system volume to the user defined values before the notification is played by using
VolumeControl.setVolume(val)
Same way you can use other widgets.
I tried to do something similar recently and I used this package. It's well documented and there's some sample code for regulating the volume. I used other packages but none work so well as this one.
You can set the volume with something like this:
late AudioManager audioManager;
dynamic maxVol,
currentVol; // Value will be int type on Android device and double type on iOS device
void setVol({int androidVol = 0, double iOSVol = 0.0}) async {
await Volume.setVol(
androidVol: androidVol,
iOSVol: iOSVol,
showVolumeUI: false,
);
}
And then just call the method where you need it and pass the values when changed (in a slider for example).
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With