Giter VIP home page Giter VIP logo

flutter_audio_capture's Introduction

flutter_audio_capture

Capture the audio stream buffer through microphone for iOS/Android. Required OS version is iOS 13+ or Android 23+

Getting Started

Add this line to your pubspec.yaml file:

dependencies:
  flutter_audio_capture: ^1.1.7

and execute

$ flutter pub get

Android

If you want to use this package on Android OS, you need to set RECORD_AUDIO permission to AndroindManifest.xml like below.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.ymd.flutter_audio_capture">
  ...
  // Add this line
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>
</manifest>

iOS

If you want to use this package on iOS, you need to set NSMicrophoneUsageDescription to Info.plist like below.

<dict>
    <key>NSMicrophoneUsageDescription</key>
    <string>Need microphone access to capture audio</string>
...

Linux

On Linux, this package uses parec to record audio.

While things should just work on recent Ubuntu versions, make sure to have pulseaudio installed on the target device.

Example

You can see full example in example/lib/main.dart

import 'package:flutter_audio_capture/flutter_audio_capture.dart';
...

// Callback function if device capture new audio stream.
// argument is audio stream buffer captured through mictophone.
// Currentry, you can only get is as Float64List.
void listener(dynamic obj) {
  var buffer = Float64List.fromList(obj.cast<double>());
  print(buffer);
}

// Callback function if flutter_audio_capture failure to register
// audio capture stream subscription.
void onError(Object e) {
  print(e);
}

...

FlutterAudioCapture plugin = new FlutterAudioCapture();
// Start to capture audio stream buffer
// sampleRate: sample rate you want
// bufferSize: buffer size you want (iOS only)
await plugin.start(listener, onError, sampleRate: 16000, bufferSize: 3000);
// Stop to capture audio stream buffer
await plugin.stop();

flutter_audio_capture's People

Contributors

clon1998 avatar davehineman avatar dynamicbutter avatar iceychris avatar srmncnk avatar ysak-y avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flutter_audio_capture's Issues

Opening app with flutter_audio_capture on IOS stops music

Hello there,

I've built an app for both Android and iOS using a particular library. However, some users have been reporting that whenever they're listening to music or watching videos and they open the app, the music or video pauses briefly. I've verified that this issue arises as soon as the flutter_audio_capture library is included in the pubspec.yaml file even if the app does not do anything with the lib/audio yet!

Thanks!

Build error on targetSdkVersion 34

Hi there,

thanks for the great package.
when I try to build my app for targetSdkVersion 34, I get this error:

* What went wrong:
A problem occurred configuring project ':flutter_audio_capture'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Specify a namespace in the module's build file: /Users/user/.pub-cache/hosted/pub.dev/flutter_audio_capture-1.1.7/android/build.gradle. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

     If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

I'd appreciate if you could update the package accordingly. Thanks in advance.

1.1.1, 1.1.2, 1.1.3 are not working on iOS

1.1.1, 1.1.2, 1.1.3 are not working on iOS. With 1.1.0, everything is good.

As you can see in the video, the microphone notification (orange circle on the top right) disappears after some time, and there is no output from the microphone in the logs.

video5783172824113876473.mp4

Here is the sample code to reproduce the issue. It looks the same as the example project:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  FlutterAudioCapture _plugin = new FlutterAudioCapture();

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

  Future<void> _startCapture() async {
    await _plugin.start(listener, onError, sampleRate: 41000, bufferSize: 3000);
  }

  Future<void> _stopCapture() async {
    await _plugin.stop();
  }

  void listener(dynamic obj) {
    print(obj);
  }

  void onError(Object e) {
    print(e);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Audio Capture Plugin'),
        ),
        body: Column(children: [
          Expanded(
              child: Row(
            children: [
              Expanded(
                  child: Center(
                      child: FloatingActionButton(
                          onPressed: _startCapture, child: Text("Start")))),
              Expanded(
                  child: Center(
                      child: FloatingActionButton(
                          onPressed: _stopCapture, child: Text("Stop")))),
            ],
          ))
        ]),
      ),
    );
  }
}

tested on iPhone 13 pro, 16.1.1

using .defaultToSpeaker causes audio session initialization problems on iPhone.

To make an iPhone play sound from the loudspeakers, not the receiver, it is necessary to add .defaultToSpeaker to the AVAudioSession options. This is causing the audio_capture session to not initialize.

Step 1:
Run the flutter_audio_capture Example without modification. Observe that the first press of Start correctly causes data to stream to the Console.

Step 2:
Modify line 21 of AudioCapture.swift as such:
options: [.defaultToSpeaker, .allowBluetoothA2DP, .allowAirPlay, .allowBluetooth])

Or change line 21 to read:
options: [.defaultToSpeaker])

Run the flutter_audio_capture Example again. Observe that nothing happens when Start is pressed.
Press Stop, then press Start again and the stream is finally seen in the Console.

Other observations:
When line 21 does not include .defaultToSpeaker, the sample rate is 44100. Adding .defaultToSpeaker changes this to 48000.
To see this, add a print statement before 'audioEngine.prepare()' .
print("Buffer: ", buffer.format, ")

I have not been able to reveal any error messages using Xcode or VScode.

Clue: The AudioKit Cookbook is able to correctly implement .defaultToSpeaker without causing iPhone problems. I am looking for differences in the code, but have not found anything that makes a difference when applied to the audio_capture plugin.
https://github.com/AudioKit/Cookbook

Tested using Xcode 14.0.1 with iPhone 7S on iOS 15.7 and iPhone XR on iOS 16.0.2.

Data format

Please what is the format/encoding of the data in the buffer. Is it PCM or AAC?

Error on versions below Android 9 (API 28)

Hi there, first of all thanks for the great package. I have a small app where I use your package, app works on iOS & Android 10 and above without any problems. But Android 9 and below, I'm getting this error on the screenshot:

Screenshot 2023-01-01 at 10 58 17

I'd appreciate if you could help. Thanks in advance.

Android Kotlin Dependency Issue

  • What went wrong:
    The Android Gradle plugin supports only Kotlin Gradle plugin version 1.5.20 and higher.
    The following dependencies do not satisfy the required version:
    project ':flutter_audio_capture' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50

Cancel error in example

@ysak-y thanks for this lightweight audio capture project. I found an issue in the example. The problem occurs when trying to restart the example after it has been stopped. On the second start I get the following error when running on iPhone 12 Pro version 14.2.1. At this point I don't know if this is a problem in the example or the plugin. I'm going to try to track down the root cause but was wondering if you may already know what it could be?

flutter: PlatformException(ON_CANCEL_FAILED, Error occured in onCancel, null, null)

Don`t work with iOS version 12.1

The documentation indicates that it is work with iOS 12+, but if you run with iOS 12.1, console show this:

Resolving dependencies of PodfileCDN: trunk Relative path: CocoaPods-version.yml exists! Returning local because checking is only perfomed in repo update [!] CocoaPods could not find compatible versions for pod "flutter_audio_capture": In Podfile: flutter_audio_capture (from.symlinks/plugins/flutter_audio_capture/ios`)

Specs satisfying the `flutter_audio_capture (from `.symlinks/plugins/flutter_audio_capture/ios`)` dependency were found, but they required a higher minimum deployment target.`

ios crash Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)

I met a crash on ios:

The crash occurred after playing audio and then starting the plugin.

I am not familiar with ios, I am not sure what is the best way to fix it.
but I fount that adding this code at the beginning of startSession in AudioCapture.swift will prevent the crash.

          if(inputFormat.channelCount == 0){
             NSLog("Not enough available inputs!")
             return
         }

here is the log:

*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)'
*** First throw call stack:
(
0 CoreFoundation 0x000000011822e28d __exceptionPreprocess + 242
1 libobjc.A.dylib 0x000000011258f894 objc_exception_throw + 48
2 CoreFoundation 0x000000011822e097 +[NSException raise:format:] + 0
3 AVFAudio 0x00000001281e08a9 _Z19AVAE_RaiseExceptionP8NSStringz + 156
4 AVFAudio 0x000000012823bf24 _ZN17AUGraphNodeBaseV318CreateRecordingTapEmjP13AVAudioFormatU13block_pointerFvP16AVAudioPCMBufferP11AVAudioTimeE + 766
5 AVFAudio 0x00000001282a89c3 -[AVAudioNode installTapOnBus:bufferSize:format:block:] + 1456
6 flutter_audio_capture 0x00000001101b1071 $s21flutter_audio_capture12AudioCaptureC12startSession10bufferSize10sampleRate2cbys6UInt32V_SdySo24FlutterStandardTypedDataCSg_Sds5Error_pSgtctKF + 673
7 flutter_audio_capture 0x00000001101b45bd $s21flutter_audio_capture30AudioCaptureEventStreamHandlerC8onListen13withArguments9eventSinkSo12FlutterErrorCSgypSg_yAJctF + 1245
8 flutter_audio_capture 0x00000001101b50ae $s21flutter_audio_capture30AudioCaptureEventStreamHandlerC8onListen13withArguments9eventSinkSo12FlutterErrorCSgypSg_yAJctFTo + 222
9 Flutter 0x000000012545d977 ___ZL39SetStreamHandlerMessageHandlerOnChannelPU31objcproto20FlutterStreamHandler8NSObjectP8NSStringPU33objcproto22FlutterBinaryMessengerS_PU29objcproto18FlutterMethodCodecS_PU27objcproto16Flutte 10 Flutter 0x0000000124e140e1 ___ZN7flutter25PlatformMessageHandlerIos21HandlePlatformMessageENSt3_fl10unique_ptrINS_15PlatformMessageENS1_14default_deleteIS3_EEEE_block_invoke + 94
11 libdispatch.dylib 0x000000011d761747 _dispatch_call_block_and_release + 12
12 libdispatch.dylib 0x000000011d7629f7 _dispatch_client_callout + 8
13 libdispatch.dylib 0x000000011d772856 _dispatch_main_queue_drain + 1362
14 libdispatch.dylib 0x000000011d7722f6 _dispatch_main_queue_callback_4CF + 31
15 CoreFoundation 0x000000011818a850 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9
16 CoreFoundation 0x000000011818518b __CFRunLoopRun + 2463
17 CoreFoundation 0x0000000118184409 CFRunLoopRunSpecific + 557
18 GraphicsServices 0x00000001220b7187 GSEventRunModal + 137
19 UIKitCore 0x000000014d3263a2 -[UIApplication _run] + 972
20 UIKitCore 0x000000014d32ae10 UIApplicationMain + 123
21 Runner 0x00000001047f3a6f main + 63
22 dyld 0x000000010ee663ee start_sim + 10
23 ??? 0x0000000206b03366 0x0 + 8702145382
)
libc++abi: terminating due to uncaught exception of type NSException

Several files should be excluded from version control

@ysak-y the goal is to remove files from version control to make it easier for other developers to contribute. That being said I'm still trying to sort out what should be checked in and what should be generated myself. From what I can tell we should not checkin the following things...

  • .idea/
  • example/ios/Podfile.lock
  • example/pubspec.lock
  • pubspec.lock

Also updating the .gitignore as described here: https://github.com/flutter/flutter/blob/master/packages/flutter_tools/templates/app/.gitignore.tmpl

Can't build in release mode

when is run 'flutter build ios', i got this:

Building com.example.example for device (ios-release)...
Warning: Missing build name (CFBundleShortVersionString).
Warning: Missing build number (CFBundleVersion).
Action Required: You must set a build name and number in the pubspec.yaml file
version field before submitting to the App Store.
Automatically signing iOS for device deployment using specified development team
in Xcode project: 9CMKDD4ZVM
Running pod install...                                           1,149ms
Running Xcode build...                                                  
 └─Compiling, linking and signing...                        537ms
Xcode build done.                                            5.3s
Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    /Users/xxxx/Documents/flutter_audio_pitch/ios/Runner/GeneratedPlugi
    nRegistrant.m:10:9: fatal error: module 'flutter_audio_capture' not found
    @import flutter_audio_capture;
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Analyzing workspace
    note: Constructing build description
    note: Build preparation complete
    /Users/xxxx/Documents/flutter_audio_pitch/ios/Runner.xcodeproj:
    warning: The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to
    8.0, but the range of supported deployment target versions is 9.0 to
    14.5.99. (in target 'Runner' from project 'Runner')

could anyone please tell me how to fix this error, thx

2 seconds delay on Linux

First off, thank you very much for this package!

When using this on linux I am encountering a 2 second delay, in the sense that the listener is only called every 2 seconds and then with many buffers in quick succession, instead of continuously. The sample rate and buffer settings have no effect on this 2 second delay. Is this intended behaviour on linux? On android I am getting each buffer one by one, as expected.

I am running Ubuntu 22.04.04 with pulseaudio 15.99.1

Flutter audio capture disappears

Hi,
I have installed Flutter_Audio_capture. However, when I click on 'Start' I get the message 'Lost connection to device'. This happens in the Android Emulator (API29). When running on a IOS device in the emulator, I get the message 'cocoapods not installed'. I have tried to get these installed, but couldn't get any further than the message that I do not have writing permission for Library/Ruby/Gems/2.6.0. directory. Don't know what to do next. Flutter doctor announces that I don't have Flutter en Dart plugins installed (which I do), and of course mentions the missing cocoapods.
Willem

Consider making `FlutterAudioCapture.start`'s arg types more specific

The main issue is the listener function, but the same also might apply to onError. listener should have type void Function(Float32List).

It's not obvious from the documentation what type of data is passed to listener. I had to figure this out by running my app and printing the runtimeType. The runtimeType was _Float32ArrayView, so then I had to do some more digging to figure out that this is a subclass of Float32List, which is the actual type I should be using in my function.

So as a first step you could mention that in the documentation, but it would be better to be more specific about the listener's type, since that moves a bunch of runtime type checks to compile time, which is better for developers and improves performance.

[BUG?] Doesn't work on Android

Redirect from: #24 (comment)

Could you please more information (e.g. flutter version, Android device version, source code (if you can provide) and so on...) @synchronisator ?

When I run example application in example directory with Android emulator (API 33), it seems working. Flutter version is 3.16.0 .

iOS error: message sent on non-platform thread

To reproduce, create a new Flutter app or run example code from flutter_audio_capture.
New Flutter app adjustments:
Add flutter_audio_capture: ^1.1.6 to pub spec.yaml dependencies.
Add NSMicrophoneUsageDescription
Need microphone access to capture audio
to info.plist.
Remove 'const' from /test/widget_test.dart line 16.
Replace main.dart code of new app with main.dart code from flutter_audio_capture/example/lib/main.dart
https://github.com/ysak-y/flutter_audio_capture/blob/master/example/lib/main.dart
To avoid clogging the terminal window, comment out line 30 "print(obj);"
Search for IPHONEOS_DEP and confirm that deployment target is at least 16.0

Using an actual device, run the code. The terminal issues an error:
[ERROR:flutter/shell/common/shell.cc(1055)] The 'ymd.dev/audio_capture_event_channel' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.

The good news is that the plugin is functioning perfectly in my app. However, I don't believe the App Store will accept an app with this issue.

Error when calling stoping the recording

//Need help//
Every time im stopping the recording i get the error: write() failed. Data transfer aborted (broken pipe)

Im using a new flutter project with just this package and both the start and the stop method inside as well as the listener and onError.
The start method is being called like this:

await plugin.start(
listener,
onError,
sampleRate: 16000,
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.