flutter_audio_output

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

Getting Started

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_audio_output/flutter_audio_output.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 {
    FlutterAudioOutput.setListener(() async {
      print("-----changed-------");
      await _getInput();
      setState(() {});
    });

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

  _getInput() async {
    _currentInput = await FlutterAudioOutput.getCurrentOutput();
    print("current:$_currentInput");
    _availableInputs = await FlutterAudioOutput.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 FlutterAudioOutput.changeToSpeaker();
              print("change to speaker:$res");
            } else {
              res = await FlutterAudioOutput.changeToReceiver();
              print("change to receiver:$res");
            }
            await _getInput();
          },
        ),
      ),
    );
  }
}

GitHub

View Github