1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to keep Flutter microphone recording active when the app is in the background?

Discussão em 'Mobile' iniciado por Stack, Outubro 3, 2024 às 11:13.

  1. Stack

    Stack Membro Participativo

    I'm building an audio recording application using Flutter, where I want to record audio continuously, even when the app is in the background. I'm using the flutter_sound and path_provider packages for audio recording, as seen in my code below:

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

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

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

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

    class _AudioRecorderWidgetState extends State<AudioRecorderWidget> {
    final FlutterSoundRecorder _recorder = FlutterSoundRecorder();
    final FlutterSoundPlayer _player = FlutterSoundPlayer();
    bool isRecording = false;
    String? filePath;

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

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

    Future<void> startRecording() async {
    try {
    Directory appDocDir = await getApplicationDocumentsDirectory();
    filePath = '${appDocDir.path}/recorded_audio.aac';
    await _recorder.startRecorder(toFile: filePath);
    setState(() {
    isRecording = true;
    });
    } catch (e) {
    print("Error starting recording: $e");
    }
    }

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

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

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

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(title: Text('Audio Recorder')),
    body: Center(
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
    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(),
    ),
    ],
    ),
    ),
    );
    }
    }



    Problem

    When I run this app, the microphone input stops picking up any sound after a few seconds when the app goes into the background. As soon as I open the app again, the microphone resumes recording correctly. What I’ve Tried

    I've read about using Picture-in-Picture mode to keep the app "active," but I would like to explore other options.
    I've looked into Flutter's documentation but couldn't find a way to keep the audio recording active in the background.


    Requirements

    I want to continue recording audio even when the app is in the background.
    I'm aware that iOS and Android have some restrictions regarding background services for battery optimization, but I'd like to know if there is a workaround or permission that allows continuous microphone usage.


    My Environment

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


    Question

    How can I keep the audio recording active when the app is in the background? Are there permissions, settings, or any specific packages that I should use to achieve this behavior in Flutter?

    Any guidance or solutions would be much appreciated!

    Continue reading...

Compartilhe esta Página