flutter_pcm_sound 3.1.1 copy "flutter_pcm_sound: ^3.1.1" to clipboard
flutter_pcm_sound: ^3.1.1 copied to clipboard

Send real-time PCM audio (16-bit integer) to your device speakers

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_pcm_sound/flutter_pcm_sound.dart';

const int sampleRate = 44100;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PcmSoundApp();
  }
}

class PcmSoundApp extends StatefulWidget {
  @override
  _PcmSoundAppState createState() => _PcmSoundAppState();
}

class _PcmSoundAppState extends State<PcmSoundApp> {
  int _remainingFrames = 0;
  MajorScale scale = MajorScale(sampleRate: sampleRate, noteDuration: 0.20);

  @override
  void initState() {
    super.initState();
    FlutterPcmSound.setLogLevel(LogLevel.verbose);
    FlutterPcmSound.setup(sampleRate: sampleRate, channelCount: 1);
    if (Platform.isAndroid) {
      FlutterPcmSound.setFeedThreshold(-1);
    } else {
      FlutterPcmSound.setFeedThreshold(sampleRate ~/ 30);
    }
    FlutterPcmSound.setFeedCallback(_onFeed);
  }

  @override
  void dispose() {
    super.dispose();
    FlutterPcmSound.release();
  }

  void _onFeed(int remainingFrames) async {
    setState(() {
      _remainingFrames = remainingFrames;
    });
    if (remainingFrames < 6000) {
      List<int> frames = scale.generate(periods: 10);
      await FlutterPcmSound.feed(PcmArrayInt16.fromList(frames));
    }
  }

  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('Flutter PCM Sound'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ElevatedButton(
                onPressed: () {
                  FlutterPcmSound.setFeedCallback(_onFeed);
                  _onFeed(0); // start feeding
                },
                child: Text('Play'),
              ),
              ElevatedButton(
                onPressed: () {
                  FlutterPcmSound.setFeedCallback(null); // stop
                  setState(() {
                    _remainingFrames = 0;
                  });
                },
                child: Text('Stop'),
              ),
              Text('$_remainingFrames Remaining Frames')
            ],
          ),
        ),
      ),
    );
  }
}
18
likes
150
points
769
downloads

Publisher

verified publisherjamcorder.com

Weekly Downloads

Send real-time PCM audio (16-bit integer) to your device speakers

Repository (GitHub)
View/report issues

Documentation

API reference

License

Unlicense (license)

Dependencies

flutter

More

Packages that depend on flutter_pcm_sound