在Flutter中更改Android设备的声音模式

时间:2019-09-22 19:57:19

标签: android flutter

我正在寻找一种在Flutter应用程序中更改设备声音模式的方法。

因此,如果用户已将设备设置为静音模式,则该应用将有权对其进行更改。

我该如何应对?

有没有可以使用的插件?

我知道在React Native中有一个插件:react-native-ringer-mode

那个剂量正好是,我该如何在Flutter中做到这一点?

1 个答案:

答案 0 :(得分:0)

请使用此软件包https://pub.dev/packages/volume
https://pub.dev/packages/vibration包装的振动和振动

完整代码

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

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  AudioManager audioManager;
  int maxVol, currentVol;


  @override
  void initState() {
    super.initState();
    audioManager = AudioManager.STREAM_SYSTEM;
    initPlatformState();
    updateVolumes();
  }

  Future<void> initPlatformState() 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);
  }

  @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>[
              DropdownButton(
                value: audioManager,
                items: [
                  DropdownMenuItem(
                    child: Text("In Call Volume"),
                    value: AudioManager.STREAM_VOICE_CALL,
                  ),
                  DropdownMenuItem(
                    child: Text("System Volume"),
                    value: AudioManager.STREAM_SYSTEM,
                  ),
                  DropdownMenuItem(
                    child: Text("Ring Volume"),
                    value: AudioManager.STREAM_RING,
                  ),
                  DropdownMenuItem(
                    child: Text("Media Volume"),
                    value: AudioManager.STREAM_MUSIC,
                  ),
                  DropdownMenuItem(
                    child: Text("Alarm volume"),
                    value: AudioManager.STREAM_ALARM,
                  ),
                  DropdownMenuItem(
                    child: Text("Notifications Volume"),
                    value: AudioManager.STREAM_NOTIFICATION,
                  ),
                ],
                isDense: true,
                onChanged: (AudioManager aM) async {
                  print(aM.toString());
                  setState(() {
                    audioManager = aM;
                  });
                  await Volume.controlVolume(aM);
                },
              ),
              (currentVol != null || maxVol != null)
                  ? Slider(
                      value: currentVol / 1.0,
                      divisions: maxVol,
                      max: maxVol / 1.0,
                      min: 0,
                      onChanged: (double d) {
                        setVol(d.toInt());
                        updateVolumes();
                      },
                    )
                  : Container(),

              FlatButton(
                child: Text("Vol Up"),
                onPressed: (){
                  Volume.volUp();
                  updateVolumes();
                },
              ),
              FlatButton(
                child: Text("Vol Down"),
                onPressed: (){
                  Volume.volDown();
                  updateVolumes();
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

enter image description here