搜索插件、代码仓与 APP 分类

GitHub 代码仓salmanjones/volume_watcher

volume_watcher

监视 Android 和 iOS 平台上的音量变化

volume_watcher 项目封面
星标
12
复刻
29
最近推送(UTC)
2023年11月14日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

Language: English | 中文简体

volume_watcher

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

Getting Started

dependencies:
  volume_watcher: ^1.3.0

Support Methods

VolumeWatcher.platformVersion
VolumeWatcher.getMaxVolume
VolumeWatcher.getCurrentVolume
VolumeWatcher.setVolume(0.0)
VolumeWatcher.addListener((double volume) {});
VolumeWatcher.removeListener(listenerId);
//Only valid on iOS
VolumeWatcher.hideVolumeView = true;

Support Listener:

VolumeWatcher(
  onVolumeChangeListener: (double volume) {
    ///do sth.
  },
)

OR

final listenerId = VolumeWatcher.addListener((double volume) {});

// You can also cancel the listener with
VolumeWatcher.removeListener(listenerId);

Super simple to use

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

import 'package:flutter/services.dart';
import 'package:volume_watcher/volume_watcher.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 {
      VolumeWatcher.hideVolumeView = true;
      platformVersion = await VolumeWatcher.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    double initVolume = 0;
    double maxVolume = 0;
    try {
      initVolume = await VolumeWatcher.getCurrentVolume;
      maxVolume = await VolumeWatcher.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>[
              VolumeWatcher(
                onVolumeChangeListener: (double volume) {
                  setState(() {
                    currentVolume = volume;
                  });
                },
              ),
              Text("System Version=$_platformVersion"),
              Text("Maximum Volume=$maxVolume"),
              Text("Initial Volume=$initVolume"),
              Text("Current Volume=$currentVolume"),
              ElevatedButton(
                onPressed: () {
                  VolumeWatcher.setVolume(maxVolume * 0.5);
                },
                child: Text("Set the volume to: ${maxVolume * 0.5}"),
              ),
              ElevatedButton(
                onPressed: () {
                  VolumeWatcher.setVolume(maxVolume * 0.0);
                },
                child: Text("Set the volume to: ${maxVolume * 0.0}"),
              )
            ],
          ),
        ),
      ),
    );
  }
}
Salman.C.Zhang GitHub avatar
GitHub 作者

Salman.C.Zhang ↗

语言JavaDartObjective-CRubyShell

使用的依赖

依赖清单 2 项

  • flutter{"sdk":"flutter"}
  • flutter_test开发依赖{"sdk":"flutter"}

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗