flutter_audio_manager_plus

  • Support to get the current output audio device
  • Support listening to input device changes
  • Support switching audio input/output devices
  • Support flutter null safety

Getting Started

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

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

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

class _MyAppState extends State<MyApp> {
  AudioInput _currentInput = AudioInput("unknow", 0);
  List<AudioInput> _availableInputs = [];

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

  Future<void> init() async {
    FlutterAudioManagerPlus.setListener(() async {
      print("-----changed-------");
      await _getInput();
      setState(() {});
    });

    await _getInput();
    if (!mounted) return;
    setState(() {});
  }

  _getInput() async {
    _currentInput = await FlutterAudioManagerPlus.getCurrentOutput();
    print("current:$_currentInput");
    _availableInputs = await FlutterAudioManagerPlus.getAvailableInputs();
    print("available $_availableInputs");
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(10),
            child: Column(
              children: <Widget>[
                Text(
                  "current output:${_currentInput.name} ${_currentInput.port}",
                ),
                Divider(),
                Expanded(
                  child: ListView.builder(
                    itemBuilder: (_, index) {
                      AudioInput input = _availableInputs[index];
                      return Row(
                        children: <Widget>[
                          Expanded(child: Text("${input.name}")),
                          Expanded(child: Text("${input.port}")),
                        ],
                      );
                    },
                    itemCount: _availableInputs.length,
                  ),
                ),
              ],
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            bool res = false;
            if (_currentInput.port == AudioPort.receiver) {
              res = await FlutterAudioManagerPlus.changeToSpeaker();
              print("change to speaker:$res");
            } else {
              res = await FlutterAudioManagerPlus.changeToReceiver();
              print("change to receiver:$res");
            }
            await _getInput();
          },
        ),
      ),
    );
  }
}

flutter_audio_manager_plus