꿈꾸는 시스템 디자이너

Flutter Example - How to set the volume | volume plugin 본문

Tutorial/Flutter with App

Flutter Example - How to set the volume | volume plugin

독행소년 2020. 3. 23. 12:38

 

* About plugin

https://pub.dev/packages/volume

 

volume | Flutter Package

Volume plugin to control device VOLUME (Android only). Pull request for IOS implementation is welcome.

pub.dev

 

1. Add this to pubspec.yaml

dependencies:
  volume: ^0.1.0

 

2. Source Code

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

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

class VolumePage extends StatefulWidget {
  @override
  _VolumePageState createState() => _VolumePageState();
}

class _VolumePageState extends State<VolumePage> {
  AudioManager audioManager;
  int maxVol, currentVol;

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

  Future<void> initPlatformState(AudioManager am) async {
    await Volume.controlVolume(am);
  }

  void updateVolumes() async {
    // get Max Volume
    maxVol = await Volume.getMaxVol;
    // get Current Volume
    currentVol = await Volume.getVol;
    print("maxVol: $maxVol, currentVol: $currentVol");
    setState(() {});
  }

  void setVol(int i) async {
    await Volume.setVol(i);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Volume Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Text("Max Volume: $maxVol"),
                    Text("Current Volume: $currentVol")
                  ],
                ),
              ),
              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());
                  initPlatformState(am);
                  updateVolumes();
                },
              ),
              (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(),
            ],
          ),
        ),
      ),
    );
  }
}

 

Comments