lecle_volume_flutter 0.0.1+1 lecle_volume_flutter: ^0.0.1+1 copied to clipboard
A Flutter plugin to control volume in Android and iOS devices programmatically.
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:lecle_volume_flutter/lecle_volume_flutter.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late AudioManager audioManager;
dynamic maxVol,
currentVol; // Value will be int type on Android device and double type on iOS device
bool showVolumeUI = true;
@override
void initState() {
super.initState();
audioManager = AudioManager.streamSystem;
initAudioStreamType();
updateVolumes();
}
Future<void> initAudioStreamType() async {
await Volume.initAudioStream(AudioManager.streamSystem);
}
void updateVolumes() async {
// get Max Volume
maxVol = await Volume.getMaxVol;
// get Current Volume
currentVol = await Volume.getVol;
setState(() {});
}
void setVol({int androidVol = 0, double iOSVol = 0.0}) async {
await Volume.setVol(
androidVol: androidVol,
iOSVol: iOSVol,
showVolumeUI: showVolumeUI,
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
DropdownButton(
value: audioManager,
items: const [
DropdownMenuItem(
child: Text("In Call Volume"),
value: AudioManager.streamVoiceCall,
),
DropdownMenuItem(
child: Text("System Volume"),
value: AudioManager.streamSystem,
),
DropdownMenuItem(
child: Text("Ring Volume"),
value: AudioManager.streamRing,
),
DropdownMenuItem(
child: Text("Media Volume"),
value: AudioManager.streamMusic,
),
DropdownMenuItem(
child: Text("Alarm volume"),
value: AudioManager.streamAlarm,
),
DropdownMenuItem(
child: Text("Notifications Volume"),
value: AudioManager.streamNotification,
),
],
isDense: true,
onChanged: (aM) async {
if (aM != null) {
// print(aM.toString());
setState(() {
audioManager = aM as AudioManager;
});
await Volume.initAudioStream(aM as AudioManager);
updateVolumes();
}
},
),
ToggleButtons(
// renderBorder: false,
borderRadius: const BorderRadius.all(Radius.circular(10.0)),
children: const <Widget>[
Padding(
padding: EdgeInsets.all(20.0),
child: Text(
"Show UI",
),
),
Padding(
padding: EdgeInsets.all(20.0),
child: Text(
"Hide UI",
),
),
],
isSelected: [showVolumeUI == true, showVolumeUI == false],
onPressed: (i) {
setState(() {
if (i == 0) {
showVolumeUI = true;
} else if (i == 1) {
showVolumeUI = false;
}
});
},
),
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
...List.generate(
2,
(index) => index == 0
? Container(
child: Text(
'Max volume: \n${maxVol ?? 0.0}',
style: const TextStyle(fontSize: 25.0),
textAlign: TextAlign.center,
),
margin: const EdgeInsets.only(bottom: 12.0),
)
: Text(
'Current volume: \n${currentVol ?? 0.0}',
style: const TextStyle(fontSize: 25.0),
textAlign: TextAlign.center,
),
)
],
),
(currentVol != null && maxVol != null)
? Slider(
value: currentVol! / 1.0,
divisions: Platform.isAndroid
? maxVol!.toInt()
: Platform.isIOS
? maxVol! ~/ 0.1
: 0,
max: maxVol! / 1.0,
min: 0,
onChanged: (d) {
setVol(androidVol: d.toInt(), iOSVol: d);
updateVolumes();
},
)
: Container(),
// FlatButton(
// child: Text("Vol Up"),
// onPressed: (){
// Volume.volUp();
// updateVolumes();
// },
// ),
// FlatButton(
// child: Text("Vol Down"),
// onPressed: (){
// Volume.volDown();
// updateVolumes();
// },
// )
],
),
),
),
);
}
}