Controll Volume in Android programatically. No IOS Implementation yet . Pull Request for ios implementation are welcome.
AudioManager.STREAM_VOICE_CALL -> Controll IN CALL Volume
AudioManager.STREAM_SYSTEM -> Controll SYSTEM Volume
AudioManager.STREAM_RING -> Controll RINGER Volume
AudioManager.STREAM_MUSIC -> Controll MEDIA Volume
AudioManager.STREAM_ALARM -> Controll ALARM Volume
AudioManager.STREAM_NOTIFICATION -> Controll NOTIFICATION Volume
ShowVolumeUI.SHOW (DEFAULT) -> Show system volume UI while changing volume
ShowVolumeUI.HIDE -> Do not show system volume UI while changing volume
await Volume.controlVolume(AudioManager audioManager); // pass any stream as parameter
await Volume.getMaxVol; // returns an integer
await Volume.getVol;// returns an integer
await Volume.setVol(int i, {ShowVolumeUI showVolumeUI});
Max value of i is less than or equal to Volume.getMaxVol.
showVolumeUI is optional parameter which defaults to ShowVolumeUI.SHOW.
class _MyAppState extends State<MyApp> {
int maxVol, currentVol;
@override
void initState() {
super.initState();
audioManager = AudioManager.STREAM_SYSTEM;
initAudioStreamType();
updateVolumes();
}
Future<void> initAudioStreamType() async {
await Volume.controlVolume(AudioManager.STREAM_SYSTEM);
}
updateVolumes() async {
// get Max Volume
maxVol = await Volume.getMaxVol;
// get Current Volume
currentVol = await Volume.getVol;
setState(() {});
}
setVol(int i) async {
await Volume.setVol(i, showVolumeUI: ShowVolumeUI.SHOW);
// or
// await Volume.setVol(i, showVolumeUI: ShowVolumeUI.HIDE);
}
// To implement the volume Up and volume Down button press programatically.