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

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

我该如何应对?

有没有可以使用的插件?

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

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

最佳答案

请使用此软件包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();
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}


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

关于android - 在Flutter中更改Android设备的声音模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58052925/

10-10 20:13