volume_watcher_plus 2.1.2 copy "volume_watcher_plus: ^2.1.2" to clipboard
volume_watcher_plus: ^2.1.2 copied to clipboard

Support ios and android real-time return system volume value, maximum volume, initial volume, support set volume.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:volume_watcher_plus/volume_watcher_plus.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  double currentVolume = 0;
  double initVolume = 0;
  double maxVolume = 0;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;

    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      VolumeWatcherPlus.hideVolumeView = true;
      platformVersion = await VolumeWatcherPlus.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    double initVolume = 0;
    double maxVolume = 0;
    try {
      initVolume = await VolumeWatcherPlus.getCurrentVolume;
      maxVolume = await VolumeWatcherPlus.getMaxVolume;
    } on PlatformException {
      platformVersion = 'Failed to get volume.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
      this.initVolume = initVolume;
      this.maxVolume = maxVolume;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin Example App'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              VolumeWatcherPlus(
                onVolumeChangeListener: (double volume) {
                  setState(() {
                    currentVolume = volume;
                  });
                },
              ),
              Text("System Version=$_platformVersion"),
              Text("Maximum Volume=$maxVolume"),
              Text("Initial Volume=$initVolume"),
              Text("Current Volume=$currentVolume"),
              ElevatedButton(
                onPressed: () {
                  VolumeWatcherPlus.setVolume(maxVolume * 0.5);
                },
                child: Text("Set the volume to: ${maxVolume * 0.5}"),
              ),
              ElevatedButton(
                onPressed: () {
                  VolumeWatcherPlus.setVolume(maxVolume * 0.0);
                },
                child: Text("Set the volume to: ${maxVolume * 0.0}"),
              )
            ],
          ),
        ),
      ),
    );
  }
}
2
likes
160
points
460
downloads

Publisher

verified publisherearthbase.io

Weekly Downloads

Support ios and android real-time return system volume value, maximum volume, initial volume, support set volume.

Homepage

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

flutter

More

Packages that depend on volume_watcher_plus