forked from cph-cachet/flutter-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cph-cachet#813 from cph-cachet/dev-noise-meter
noise_meter updated to 5.0.0 using audio_streamer 4.0.0
- Loading branch information
Showing
10 changed files
with
117 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Additional information about this file can be found at | ||
# https://dart.dev/guides/language/analysis-options | ||
|
||
include: package:flutter_lints/flutter.yaml | ||
|
||
analyzer: | ||
exclude: [build/**] | ||
language: | ||
strict-casts: true | ||
strict-inference: true | ||
strict-raw-types: false | ||
|
||
linter: | ||
rules: | ||
cancel_subscriptions: true | ||
constant_identifier_names: false | ||
depend_on_referenced_packages: false | ||
avoid_print: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,3 @@ | ||
# example | ||
# Noise Meter Example | ||
|
||
A new Flutter project. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Flutter application. | ||
|
||
A few resources to get you started if this is your first Flutter project: | ||
|
||
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) | ||
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.dev/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. | ||
An example app for the noise_meter plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,98 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:noise_meter/noise_meter.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'dart:async'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
|
||
void main() { | ||
runApp(MyApp()); | ||
} | ||
void main() => runApp(NoiseMeterApp()); | ||
|
||
class MyApp extends StatefulWidget { | ||
class NoiseMeterApp extends StatefulWidget { | ||
@override | ||
_MyAppState createState() => _MyAppState(); | ||
_NoiseMeterAppState createState() => _NoiseMeterAppState(); | ||
} | ||
|
||
class _MyAppState extends State<MyApp> { | ||
class _NoiseMeterAppState extends State<NoiseMeterApp> { | ||
bool _isRecording = false; | ||
NoiseReading? _latestReading; | ||
StreamSubscription<NoiseReading>? _noiseSubscription; | ||
NoiseMeter? _noiseMeter; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_noiseMeter = NoiseMeter(onError); | ||
} | ||
NoiseMeter? noiseMeter; | ||
|
||
@override | ||
void dispose() { | ||
_noiseSubscription?.cancel(); | ||
super.dispose(); | ||
} | ||
|
||
void onData(NoiseReading noiseReading) { | ||
this.setState(() { | ||
_latestReading = noiseReading; | ||
if (!this._isRecording) this._isRecording = true; | ||
}); | ||
} | ||
void onData(NoiseReading noiseReading) => | ||
setState(() => _latestReading = noiseReading); | ||
|
||
void onError(Object error) { | ||
print(error); | ||
_isRecording = false; | ||
stop(); | ||
} | ||
|
||
void start() { | ||
try { | ||
_noiseSubscription = _noiseMeter?.noise.listen(onData); | ||
} catch (err) { | ||
print(err); | ||
} | ||
/// Check if microphone permission is granted. | ||
Future<bool> checkPermission() async => await Permission.microphone.isGranted; | ||
|
||
/// Request the microphone permission. | ||
Future<void> requestPermission() async => | ||
await Permission.microphone.request(); | ||
|
||
/// Start noise sampling. | ||
Future<void> start() async { | ||
// Create a noise meter, if not already done. | ||
noiseMeter ??= NoiseMeter(); | ||
|
||
// Check permission to use the microphone. | ||
// | ||
// Remember to update the AndroidManifest file (Android) and the | ||
// Info.plist and pod files (iOS). | ||
if (!(await checkPermission())) await requestPermission(); | ||
|
||
// Listen to the noise stream. | ||
_noiseSubscription = noiseMeter?.noise.listen(onData, onError: onError); | ||
setState(() => _isRecording = true); | ||
} | ||
|
||
/// Stop sampling. | ||
void stop() { | ||
try { | ||
_noiseSubscription?.cancel(); | ||
this.setState(() { | ||
this._isRecording = false; | ||
}); | ||
} catch (err) { | ||
print(err); | ||
} | ||
_noiseSubscription?.cancel(); | ||
setState(() => _isRecording = false); | ||
} | ||
|
||
List<Widget> getContent() => <Widget>[ | ||
Container( | ||
margin: EdgeInsets.all(25), | ||
child: Column(children: [ | ||
Container( | ||
child: Text(_isRecording ? "Mic: ON" : "Mic: OFF", | ||
style: TextStyle(fontSize: 25, color: Colors.blue)), | ||
margin: EdgeInsets.only(top: 20), | ||
), | ||
Container( | ||
child: Text( | ||
'Noise: ${_latestReading?.meanDecibel} dB', | ||
), | ||
margin: EdgeInsets.only(top: 20), | ||
), | ||
Container( | ||
child: Text( | ||
'Max: ${_latestReading?.maxDecibel} dB', | ||
), | ||
) | ||
])), | ||
]; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: getContent())), | ||
floatingActionButton: FloatingActionButton( | ||
Widget build(BuildContext context) => MaterialApp( | ||
home: Scaffold( | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Container( | ||
margin: EdgeInsets.all(25), | ||
child: Column(children: [ | ||
Container( | ||
child: Text(_isRecording ? "Mic: ON" : "Mic: OFF", | ||
style: TextStyle(fontSize: 25, color: Colors.blue)), | ||
margin: EdgeInsets.only(top: 20), | ||
), | ||
Container( | ||
child: Text( | ||
'Noise: ${_latestReading?.meanDecibel.toStringAsFixed(2)} dB', | ||
), | ||
margin: EdgeInsets.only(top: 20), | ||
), | ||
Container( | ||
child: Text( | ||
'Max: ${_latestReading?.maxDecibel.toStringAsFixed(2)} dB', | ||
), | ||
) | ||
])), | ||
])), | ||
floatingActionButton: FloatingActionButton( | ||
backgroundColor: _isRecording ? Colors.red : Colors.green, | ||
child: _isRecording ? Icon(Icons.stop) : Icon(Icons.mic), | ||
onPressed: _isRecording ? stop : start, | ||
child: _isRecording ? Icon(Icons.stop) : Icon(Icons.mic)), | ||
), | ||
); | ||
} | ||
), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,24 @@ | ||
name: noise_meter_example | ||
description: Example app for the NoiseMeter package. | ||
description: Example app for the noise_meter package. | ||
publish_to: none | ||
|
||
version: 1.0.0+1 | ||
version: 5.0.0 | ||
|
||
environment: | ||
sdk: ">=2.12.0 <3.0.0" | ||
sdk: ">=2.17.0 <4.0.0" | ||
flutter: ">=3.0.0" | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
noise_meter: | ||
path: ../ | ||
|
||
# The following adds the Cupertino Icons font to your application. | ||
# Use with the CupertinoIcons class for iOS style icons. | ||
cupertino_icons: ^1.0.2 | ||
permission_handler: ^11.0.0 | ||
|
||
dev_dependencies: | ||
flutter_test: | ||
sdk: flutter | ||
|
||
# For information on the generic Dart part of this file, see the | ||
# following page: https://dart.dev/tools/pub/pubspec | ||
|
||
# The following section is specific to Flutter. | ||
flutter: | ||
# The following line ensures that the Material Icons font is | ||
# included with your application, so that you can use the icons in | ||
# the material Icons class. | ||
uses-material-design: true | ||
|
||
# To add assets to your application, add an assets section, like this: | ||
# assets: | ||
# - images/a_dot_burr.jpeg | ||
# - images/a_dot_ham.jpeg | ||
|
||
# An image asset can refer to one or more resolution-specific "variants", see | ||
# https://flutter.dev/assets-and-images/#resolution-aware. | ||
|
||
# For details regarding adding assets from package dependencies, see | ||
# https://flutter.dev/assets-and-images/#from-packages | ||
|
||
# To add custom fonts to your application, add a fonts section here, | ||
# in this "flutter" section. Each entry in this list should have a | ||
# "family" key with the font family name, and a "fonts" key with a | ||
# list giving the asset and other descriptors for the font. For | ||
# example: | ||
# fonts: | ||
# - family: Schyler | ||
# fonts: | ||
# - asset: fonts/Schyler-Regular.ttf | ||
# - asset: fonts/Schyler-Italic.ttf | ||
# style: italic | ||
# - family: Trajan Pro | ||
# fonts: | ||
# - asset: fonts/TrajanPro.ttf | ||
# - asset: fonts/TrajanPro_Bold.ttf | ||
# weight: 700 | ||
# | ||
# For details regarding fonts from package dependencies, | ||
# see https://flutter.dev/custom-fonts/#from-packages |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.