noise_meter 1.0.2
noise_meter: ^1.0.2 copied to clipboard
A noise meter plugin for Android and iOS. Currently works for Android only, will support iOS in near future.
example/lib/main.dart
import 'package:noise_meter/noise_meter.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isRecording = false;
StreamSubscription<NoiseReading> _noiseSubscription;
NoiseMeter _noiseMeter;
@override
void initState() {
super.initState();
}
void onData(NoiseReading noiseReading) {
this.setState(() {
if (!this._isRecording) {
this._isRecording = true;
}
});
print(noiseReading.toString());
}
void startRecorder() async {
print('***NEW RECORDING***');
try {
_noiseMeter = new NoiseMeter();
_noiseSubscription = _noiseMeter.noiseStream.listen(onData);
} on NoiseMeterException catch (exception) {
print(exception);
}
}
void stopRecorder() async {
try {
if (_noiseSubscription != null) {
_noiseSubscription.cancel();
_noiseSubscription = null;
}
this.setState(() {
this._isRecording = false;
});
} catch (err) {
print('stopRecorder error: $err');
}
}
void printWrapped(String text) {
final pattern = new RegExp('.{1,800}'); // 800 is the size of each chunk
pattern.allMatches(text).forEach((match) => print(match.group(0)));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Noise Level Example"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Noise Level",
),
Text(
'MIC ${(_isRecording ? 'ON' : 'OFF')} \n(Check Flutter log for data)',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
if (!this._isRecording) {
return this.startRecorder();
}
this.stopRecorder();
},
child: Icon(this._isRecording ? Icons.stop : Icons.mic)),
),
);
}
}