android – How you can preserve Flutter microphone recording energetic when the app is within the background?

0
23
android – How you can preserve Flutter microphone recording energetic when the app is within the background?


I am constructing an audio recording software utilizing Flutter, the place I need to report audio repeatedly, even when the app is within the background. I am utilizing the flutter_sound and path_provider packages for audio recording, as seen in my code under:

import 'package deal:flutter/materials.dart';
import 'package deal:flutter_sound/flutter_sound.dart';
import 'package deal:path_provider/path_provider.dart';
import 'dart:io';

class MyApp extends StatelessWidget {
  const MyApp({tremendous.key});

  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      title: 'Audio Recorder',
      theme: ThemeData(primarySwatch: Colours.blue),
      dwelling: AudioRecorderWidget(),
    );
  }
}

class AudioRecorderWidget extends StatefulWidget {
  @override
  _AudioRecorderWidgetState createState() => _AudioRecorderWidgetState();
}

class _AudioRecorderWidgetState extends State {
  closing FlutterSoundRecorder _recorder = FlutterSoundRecorder();
  closing FlutterSoundPlayer _player = FlutterSoundPlayer();
  bool isRecording = false;
  String? filePath;

  @override
  void initState() {
    tremendous.initState();
    initAudio();
  }

  Future initAudio() async {
    await _recorder.openRecorder();
    await _player.openPlayer();
  }

  Future startRecording() async {
    strive {
      Listing appDocDir = await getApplicationDocumentsDirectory();
      filePath="${appDocDir.path}/recorded_audio.aac";
      await _recorder.startRecorder(toFile: filePath);
      setState(() {
        isRecording = true;
      });
    } catch (e) {
      print("Error beginning recording: $e");
    }
  }

  Future stopRecording() async {
    strive {
      await _recorder.stopRecorder();
      setState(() {
        isRecording = false;
      });
    } catch (e) {
      print("Error stopping recording: $e");
    }
  }

  Future playRecording() async {
    if (filePath != null) {
      await _player.startPlayer(fromURI: filePath);
    }
  }

  @override
  void dispose() {
    _recorder.closeRecorder();
    _player.closePlayer();
    tremendous.dispose();
  }

  @override
  Widget construct(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Textual content('Audio Recorder')),
      physique: Heart(
        little one: Column(
          mainAxisAlignment: MainAxisAlignment.heart,
          kids: [
            ElevatedButton(
              onPressed: isRecording ? null : startRecording,
              child: Text('Start Recording'),
            ),
            ElevatedButton(
              onPressed: isRecording ? stopRecording : null,
              child: Text('Stop Recording'),
            ),
            ElevatedButton(
              onPressed: !isRecording && filePath != null ? playRecording : null,
              child: Text('Play Recording'),
            ),
            if (isRecording)
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: CircularProgressIndicator(),
              ),
          ],
        ),
      ),
    );
  }
}

Drawback

Once I run this app, the microphone enter stops choosing up any sound after a couple of seconds when the app goes into the background. As quickly as I open the app once more, the microphone resumes recording appropriately.
What I’ve Tried

I've examine utilizing Image-in-Image mode to maintain the app "energetic," however I wish to discover different choices.
I've regarded into Flutter's documentation however could not discover a solution to preserve the audio recording energetic within the background.

Necessities

I need to proceed recording audio even when the app is within the background.
I am conscious that iOS and Android have some restrictions concerning background providers for battery optimization, however I would prefer to know if there's a workaround or permission that enables steady microphone utilization.

My Setting

Flutter model: 3.x
Plugin: flutter_sound for audio recording

Query

How can I preserve the audio recording energetic when the app is within the background? Are there permissions, settings, or any particular packages that I ought to use to realize this habits in Flutter?

Any steerage or options can be a lot appreciated!

LEAVE A REPLY

Please enter your comment!
Please enter your name here