A Flutter audio/volume plugin which provides easy access to volume/audio related info like Volume Level
for different Stream Types
, Ringer Mode
or even Audio Mode
.
Permissions
No permissions are required for the majority of this plugin in Android. However, in order to switch Do Not Disturb Mode on/off you will need to enable this permission. To do so first you need to add the following to AndroidManifest.xml
. This will make the app to appear in the Do Not Disturb Access list.
<manifest ... >
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<application ... >
...
</manifest>
For devices with Android 6.0 and above, additionally to the above it is required for the user to grant Do No Disturb Access.
To check if the user has granted the permissions and prompt for approval
import 'package:real_volume/real_volume.dart';
bool? isPermissionGranted = await RealVolume.isPermissionGranted();
if (!isPermissionGranted!) {
// Opens Do Not Disturb Access settings to grant the access
await RealVolume.openDoNotDisturbSettings();
}
getMaxVol
getMinVol
getCurrentVol
setVolume
getRingerMode
(will return either RingerMode.SILENT
or RingerMode.NORMAL
)onVolumeChanged
onRingerModeChanged
(event can be either RingerMode.SILENT
or RingerMode.NORMAL
)_NOTE1: For iOS devices
streamType
is completely ignored when it passed as an argument and null when it is returned._NOTE2: For iOS devices if you call
getRingerMode
AFTER you have subscribed toonRingerModeChanged
listener the listener will stop working. So keep in mind that if you want to use both of them in the same project you need to cancel and re-subscribe toonRingerModeChanged
listener AFTER you have calledgetRingerMode
function.
Get Ringer Mode
:
import 'package:real_volume/real_volume.dart';
RingerMode? ringerMode = await RealVolume.getRingerMode();
print(ringerMode?.name ?? 'Unknown');
Set Ringer Mode
:
import 'package:real_volume/real_volume.dart';
// It will redirect the user to grant DND access if 'redirectIfNeeded' is set to true(default behavior)
await RealVolume.setRingerMode(RingerMode.SILENT, redirectIfNeeded: false);
Get current Volume Level
for Notifications :
import 'package:real_volume/real_volume.dart';
double notificationVolume = (await RealVolume.getCurrentVol(StreamType.NOTIFICATION)) ?? 0.0;
print(notificationVolume+'');
Set current Volume Level
for Music :
import 'package:real_volume/real_volume.dart';
await RealVolume.setVolume(0.75, showUI: true, streamType: StreamType.MUSIC);
Get Audio Mode
:
import 'package:real_volume/real_volume.dart';
AudioMode? audioMode = await RealVolume.getAudioMode();
print(audioMode?.name ?? 'Unknown');
Listener for Volume level
:
import 'package:real_volume/real_volume.dart';
RealVolume.onVolumeChanged.listen((event) async {
setState(() {
currentVolume = event.volumeLevel;
selectedStreamType = event.streamType;
});
});
Audio Mode
Set Audio Mode
works for every case