record

repository·main·Indexed 43 days ago

https://github.com/llfbandit/record

A multi-platform audio recording library for Flutter that supports recording from a microphone to a file path or a stream. It provides support for AAC, Opus, WAV, and PCM encoders across Android, iOS, macOS, Windows, Linux, and Web.

Tokens
6.9K
Snippets
23
Records
32
Agent score
56%

What's inside record

  1. Overview of record_web

    main

    The record_web package provides the web-specific implementation for the record package. It is invoked via the record_platform_interface.

    Key Behavior: If the requested sample rate is not supported natively by the browser/web environment, the package automatically handles resampling of the audio buffers within the audio thread to ensure compatibility.

  2. Understand Android audio recording limitations and effects

    main

    When using record on Android, be aware of the following hardware and software behaviors:

    • Audio Effects: Features like auto gain, echo cancellation, and noise suppression are hardware-dependent and may be unavailable on certain devices.
    • Volume Control: There are no gain settings available via the API; you cannot programmatically control the input volume. Low recording volume is typically a hardware characteristic of the specific device.
    • Volume Impact of Effects: Enabling audio effects will likely lower the output volume.
    • Source Selection: Choosing an audio source other than default or mic will likely result in lower output volume.
  3. Quickstart: Record audio to a file or stream

    main

    To record audio using record, instantiate an AudioRecorder, check for permissions, and then call start (for files) or startStream (for streams). Always call dispose() when the recorder is no longer needed to free resources.

    Recording to a file

    Use record.start(RecordConfig, path: '...') to save audio directly to a specific file path.

    Recording to a stream

    Use record.startStream(RecordConfig) to receive a stream of audio data. This is useful for real-time processing or uploading data as it is captured.

    Stopping and Canceling

    • record.stop(): Stops recording and returns the file path (or a blob URL on Web).
    • record.cancel(): Stops recording and implicitly removes the file or blob created.
    import 'package:record/record.dart';
    
    final record = AudioRecorder();
    
    // Check and request permission if needed
    if (await record.hasPermission()) {
      // Start recording to file
      await record.start(const RecordConfig(), path: 'aFullPath/myFile.m4a');
      
      // ... or to stream
      final stream = await record.startStream(const RecordConfig(encoder: AudioEncoder.pcm16bits));
    }
    
    // Stop recording...
    final path = await record.stop();
    // ... or cancel it (and implicitly remove file/blob).
    await record.cancel();
    
    record.dispose(); // As always, don't forget this one.
  4. Run native tests for record_windows

    main

    To run the native C++ tests for the record_windows implementation, you must first build the example app and then use CMake to configure and build the test target. These tests are located within the record/example directory.

    flutter build windows --debug
    cmake -S windows -B build/windows/x64 -Dinclude_record_windows_tests=ON
    cmake --build build/windows/x64 --config Debug --target record_windows_test
    build\windows\x64\plugins\record_windows\Debug\record_windows_test.exe
  5. Configure dependencies for Linux

    main

    On Linux, encoding is provided by external tools that must be installed separately. The package relies on parecord, pactl, and ffmpeg.

    • parecord: Used for audio input.
    • pactl: Used for utility methods like getting available devices.
    • ffmpeg: Used for encoding and output.

    On Ubuntu 24.04.3 LTS, you can install these dependencies using:

    sudo apt install pulseaudio-utils ffmpeg
  6. Configure iOS background audio permissions

    main

    To allow the record plugin to function while the app is in the background on iOS, add the UIBackgroundModes key with audio and fetch strings to your ios/Runner/info.plist file.

    <key>UIBackgroundModes</key>
    <array>
      <string>audio</string>
      <string>fetch</string>
    </array>
  7. Handle audio blobs on Web

    main
    When using record on the Web, calling stop() returns a blob URL that keeps the recording in memory. To prevent memory leaks, you should revoke the URL using URL.revokeObjectURL once you have finished using the recording.
  8. Implement background recording on Android and iOS

    main

    The record plugin does not natively support background recording. To achieve background recording, you must use an external package to manage the application lifecycle in the background.

    Android

    For Android, it is recommended to use an external package like flutter_foreground_task to maintain a foreground service.

    iOS

    For iOS, if your requirements are limited, you can enable background audio by configuring your Info.plist.

    Note: If you are using AudioInterruptionMode.pauseResume, you must include IosAudioCategoryOptions.mixWithOthers to ensure recording continues correctly after audio session interruptions.

    <key>UIBackgroundModes</key>
    <array>
      <string>audio</string>
      <string>fetch</string>
    </array>
  9. Configure permissions for iOS and macOS

    main

    Both iOS and macOS require a microphone usage description in your Info.plist file to request permission from the user.

    iOS

    Add the following to ios/Runner/Info.plist:

    <key>NSMicrophoneUsageDescription</key>
    <string>Some message to describe why you need this permission</string>

    macOS

    1. Add the following to macos/Runner/Info.plist:
    <key>NSMicrophoneUsageDescription</key>
    <string>Some message to describe why you need this permission</string>
    1. In your project capabilities, activate "Audio input" in both debug and release schemes, or add the following key directly to your *.entitlements files:
    <key>com.apple.security.device.audio-input</key>
    <true/>
  10. Implement a new platform for the record plugin

    main

    To add support for a new platform in the record plugin, you must implement the RecordPlatform interface. This ensures your platform-specific implementation adheres to the expected contract used by the main plugin.

    1. Create a class that extends RecordPlatform.
    2. Implement the required platform-specific behaviors within your class.
    3. Register your implementation as the default platform by assigning it to RecordPlatform.instance during plugin registration.
    // 1. Extend RecordPlatform
    class MyRecordPlatform extends RecordPlatform {
      // Implement platform-specific behavior here
    }
    
    // 2. Register the implementation
    RecordPlatform.instance = MyRecordPlatform();
  11. Customize iOS launch screen assets

    main

    To change the launch screen image for your iOS application, you can either replace the image files directly in the ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    Using Xcode:

    1. Open your Flutter project's iOS workspace using the command: open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch screen assets.
    open ios/Runner.xcworkspace