Plugin name & version
Noise Meter (v1.0.2)
Describe the bug
The plugin works fine if an app is created only to run a demo of the plugin. If the plugin is used in an app (in a nested Widget tree), then the Audio Recorder cannot be initialized.
To Reproduce
`class LeftSide extends StatefulWidget {
@override
_LeftSideState createState() => _LeftSideState();
}
class _LeftSideState extends State<LeftSide> {
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
SizedBox(
width: 20
),
Stack(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Container(
width: MediaQuery.of(context).size.width*0.2,
height: MediaQuery.of(context).size.height*0.6,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: Colors.black26,
borderRadius: BorderRadius.all(Radius.circular(15)),
border: Border.all(color: Colors.white10),
boxShadow: [
BoxShadow(
color: Colors.black26,
offset: new Offset(7.0, 7.0),
blurRadius: 5.0,
)
],
),
),
),
Positioned(
top: 45,
left: 55,
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black26,
border: Border.all(color: Colors.white10),
boxShadow: [
BoxShadow(
color: Colors.black26,
//offset: new Offset(7.0, 7.0),
blurRadius: 5.0,
spreadRadius: 5.0
)
],
),
child: Opacity(opacity: 0.5, child: Icon(Icons.music_note, size: 20, color: Color(0xFF7BA0F9))),
),
),
Positioned(
bottom: 45,
left: 55,
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black26,
border: Border.all(color: Colors.white10),
boxShadow: [
BoxShadow(
color: Colors.black26,
//offset: new Offset(7.0, 7.0),
blurRadius: 5.0,
spreadRadius: 5.0
)
],
),
child: MicGauge(),
),
),
],
),
],
);
}
}`
class MicGauge extends StatefulWidget {
@override
_MicGaugeState createState() => _MicGaugeState();
}
class _MicGaugeState extends State<MicGauge> {
bool _isRecording = false;
StreamSubscription<NoiseReading> _noiseSubscription;
NoiseMeter _noiseMeter;
double dbLevel = 0;
@override
void initState() {
super.initState();
//startRecorder();
}
@override
void dispose() {
super.dispose();
_noiseSubscription.cancel();
}
void onData(NoiseReading noiseReading) {
this.setState(() {
if (!this._isRecording) {
this._isRecording = true;
}
});
//print(noiseReading.db);
this.dbLevel = noiseReading.db;
print(this.dbLevel);
}
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');
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
startRecorder();
},
child: Text(this.dbLevel.toString())
);
}
}
The following lines have been added to the Android Manifest file:
<uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Expected behavior
The Noise Meter should update the Text Widget for decibel level in real time.
Actual behavior
The Noise Meter plugin works fine when the code is implemented as is (like in the example of Noise Meter repo), but does not work when implemented in a nested type widget tree as shown above. Given below are the details of the error.
I/flutter ( 6610): ***NEW RECORDING*** E/IAudioFlinger( 6610): createRecord returned error -22 E/AudioRecord( 6610): AudioFlinger could not create record track, status: -22 E/AudioRecord-JNI( 6610): Error creating AudioRecord instance: initialization check failed with status -22. E/android.media.AudioRecord( 6610): Error code -20 when initializing native AudioRecord object. E/NoiseCalibration( 6610): Audio Record can't initialize!
Flutter doctor
[√] Flutter (Channel stable, v1.12.13+hotfix.8, on Microsoft Windows [Version 10.0.18363.719], locale en-US)
• Flutter version 1.12.13+hotfix.8 at c:\src\flutter
• Framework revision 0b8abb4724 (8 weeks ago), 2020-02-11 11:44:36 -0800
• Engine revision e1e6ced81d
• Dart version 2.7.0
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at C:\Users\Ankush\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[√] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 45.0.1
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[√] IntelliJ IDEA Community Edition (version 2019.3)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.1
• Flutter plugin version 42.1.4
• Dart plugin version 193.5731
[√] Connected device (1 available)
• SM A205F • RZ8M92CX7PJ • android-arm64 • Android 9 (API 28)
• No issues found!
Smartphone(s) / Emulator
Please complete the following information for each phone and/or emulator you're experiencing this bug on:
- Device: [Samsung SMA205F]
- OS: [Android Pie]