The status of the Flutter button is not updated

54 views Asked by At

I use the following packages:

flutter_sound_lite: ^8.1.9 permission_handler: ^8.1.2

I have a window that records audio using the flutter_sound_lite library and a Start button. When you click on the button, its text should change to Stop. When you turn it on for the first time, however, when you turn on the button on this page, the inscription remains in the Stop position.

How can this problem be solved?

 Widget buildStart(BuildContext context) {
    String text = recorder.isRecording ? 'Stop' : 'Start';

    return Center(
      child: Column(
        children: [
          timerWidget(controller: controller,),
          InkWell(
            onTap: () async {
              await recorder.toggleRecording();
              final isRecording = recorder.isRecording;
              print("Bool Recording : " + recorder.isRecording.toString());

              setState(() {

              });

              if(isRecording){
                controller.startTimer();
              }else{
                controller.stopTimer();
              }
            },
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 38.0),
              height: 60.0,
              decoration: BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                    color: Color(0xFFD7DCE1).withOpacity(0.25),
                    offset: Offset(0.0, 1.0), //(x,y)
                    blurRadius: 20.0,
                  ),
                ],
                borderRadius: BorderRadius.circular(100),
              ),
              child: Center(
                child: Text(
                  text,
                  style: headline1.copyWith(
                    color: dark,
                  ),
                ),
              ),
            ),
          ),

        ],
      ),
    );
import 'dart:typed_data';
import 'dart:io';

import 'package:flutter_sound_lite/flutter_sound.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:path_provider/path_provider.dart';

final pathToSaveAudio = 'audiotiamo';

class soundsRecorder{
  FlutterSoundRecorder ? _audioRecorder;
  bool _isRecorderInitialised = false;

  bool get isRecording => _audioRecorder!.isRecording;

  Future init() async
  {
    _audioRecorder = FlutterSoundRecorder();

    final status = await Permission.microphone.request();
    if(status != PermissionStatus.granted){
      throw RecordingPermissionException('Microphone permission');
    }

    final statusStorage = await Permission.storage.status;
    if (!statusStorage.isGranted) {
      await Permission.storage.request();
    }

    await _audioRecorder!.openAudioSession();
    _isRecorderInitialised = true;

    directoryPath = await _directoryPath();
    completePath = await _completePath(directoryPath);
    //_createDirectory();
    //_createFile();
  }

  void dispose()
  {
    if(!_isRecorderInitialised) return;

    _audioRecorder!.closeAudioSession();
    _audioRecorder = null;
    _isRecorderInitialised = false;
  }

  Future _record() async
  {
    if(!_isRecorderInitialised) return;
    print("Path where the file will be : "+completePath);

    await _audioRecorder!.startRecorder(
      toFile: completePath,
      numChannels : 1,
      sampleRate: 44100,
    );
  }

  Future _stop() async
  {
    if(!_isRecorderInitialised) return;
    await _audioRecorder!.stopRecorder();
    File f = File(completePath);
    print("The created file : $f");
  }

  Future toggleRecording() async
  {
    if (_audioRecorder!.isStopped){
      await _record();
    } else {
      _stop();
    }
  }

  Future isStarted() async
  {
    if (_audioRecorder!.isStopped){
      return true;
    } else {
      return false;
    }
  }

  String completePath = "";
  String directoryPath = "";
  DateTime now = new DateTime.now();

  Future<String> _completePath(String directory) async {
    var fileName = _fileName();
    return "$directory$fileName";
  }

  Future<String> _directoryPath() async {
    var directory = await getExternalStorageDirectory();
    var directoryPath = directory!.path;
    return "$directoryPath/records/";
  }

  String _fileName() {
    return "record " + DateTime.now().toString() + ".wav";
  }


  Future _createFile() async {
    File(completePath)
        .create(recursive: true)
        .then((File file) async {
      //write to file
      Uint8List bytes = await file.readAsBytes();
      file.writeAsBytes(bytes);
      print("FILE CREATED AT : "+file.path);
    });
  }

  void _createDirectory() async {
    bool isDirectoryCreated = await Directory(directoryPath).exists();
    if (!isDirectoryCreated) {
      Directory(directoryPath).create()
          .then((Directory directory) {
        print("DIRECTORY CREATED AT : " +directory.path);
      });
    }
  }
}

I think the problem is in bool get isRecording => _audioRecorder!.isRecording;

Maybe that's not how I address it, but after the first iteration, I always get the true value

0

There are 0 answers