Flutter Sound recording onProgress stream returning null

736 Views Asked by At

I want to visualize the recorded sound intensity in my flutter app using flutter_sound. I listen on the onProgress stream in a future builder which should return the value decibel when the recording is on. The issue is the stream always returns null!

  StreamBuilder<RecordingDisposition>(
                  stream: _recorder!.onProgress,
                  builder: (context, snapshot) {
                    log.debug(snapshot.data);
                    if (!snapshot.hasData || snapshot.data == null) {
                      return Container(
                        width: 120,
                        color: Colors.green,
                      );
                    }
                    return Container(
                        width: 120,
                        child: Container(
                          color: Colors.red,
                          width: snapshot.data!.decibels,
                        ));
                  }),

And here is the code I initialize the recorder in it.

  Future<void> openTheRecorder() async {
var status = await Permission.microphone.request();
if (status != PermissionStatus.granted) {
  throw RecordingPermissionException('Microphone permission not granted');
}

await _recorder!.openAudioSession(
  focus: AudioFocus.requestFocusAndDuckOthers,
  category: SessionCategory.record,
);


 this.record();
}
1

There are 1 best solutions below

0
sergeyne On

You need to set non zero value for subscription duration

await _recorder!.setSubscriptionDuration(
      const Duration(milliseconds: 100),
    );