GithubHelp home page GithubHelp logo

davidmr001 / flutter_sound Goto Github PK

View Code? Open in Web Editor NEW

This project forked from canardoux/flutter_sound

0.0 1.0 0.0 508 KB

Flutter plugin for sound. Audio recorder and player.

License: MIT License

Java 32.89% Ruby 5.31% Objective-C 26.87% Dart 34.93%

flutter_sound's Introduction

flutter_sound

pub version

This plugin provides simple recorder and player functionalities for both `android` and `ios` platforms. This only supports default file extension for each platform. This plugin handles file from remote url. This plugin can handle playback stream from native (To sync exact time with bridging).

Free Read

Medium Blog

Getting Started

For help getting started with Flutter, view our online documentation.

Install

Add flutter_sound as a dependency in pubspec.yaml For help on adding as a dependency, view the documentation.

Post Installation

On iOS you need to add a usage description to info.plist:

<key>NSMicrophoneUsageDescription</key>
<string>This sample uses the microphone to record your speech and convert it to text.</string>
<key>UIBackgroundModes</key>
<array>
	<string>audio</string>
</array>

On Android you need to add a permission to AndroidManifest.xml:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Methods

Func Param Return Description
setSubscriptionDuration double sec String message Set subscription timer in seconds. Default is 0.01 if not using this method.
startRecorder String uri, int sampleRate, int numChannels String uri Start recording. This will return uri used.
stopRecorder String message Stop recording.
startPlayer String uri String message Start playing.
stopPlayer String message Stop playing.
pausePlayer String message Pause playing.
resumePlayer String message Resume playing.
seekToPlayer int milliSecs position to goTo String message Seek audio to selected position in seconds. Parameter should be less than audio duration to correctly placed.

Subscriptions

Subscription Return Description
onRecorderStateChanged <RecordStatus> Able to listen to subscription when recorder starts.
onPlayerStateChanged <PlayStatus> Able to listen to subscription when player starts.

Default uri path

When uri path is not set during the function call in startRecorder or startPlayer, they are saved in below path depending on the platform.

  • Default path for android
    • sdcard/sound.mp4.
  • Default path for ios
    • sound.m4a.

Usage

Creating instance.

FlutterSound flutterSound = new FlutterSound();

Starting recorder with listener.

String path = await flutterSound.startRecorder(null);
print('startRecorder: $path');

_recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
  DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
  String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
});

Stop recorder

String result = await flutterSound.stopRecorder();
print('stopRecorder: $result');

if (_recorderSubscription != null) {
	_recorderSubscription.cancel();
	_recorderSubscription = null;
}

Start player

String path = await flutterSound.startPlayer(null);
print('startPlayer: $path');

_playerSubscription = flutterSound.onPlayerStateChanged.listen((e) {
	if (e != null) {
		DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
		String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
		this.setState(() {
			this._isPlaying = true;
			this._playerTxt = txt.substring(0, 8);
		});
	}
});

Stop player

String result = await flutterSound.stopPlayer();
print('stopPlayer: $result');
if (_playerSubscription != null) {
	_playerSubscription.cancel();
	_playerSubscription = null;
}

Pause player

String result = await flutterSound.pausePlayer();

Resume player

String result = await flutterSound.resumePlayer();

Seek player

String result = await flutterSound.seekToPlayer(miliSecs);

Setting subscription duration (Optional). 0.01 is default value when not set.

/// 0.01 is default
flutterSound.setSubscriptionDuration(0.01);

Setting volume.

/// 1.0 is default
/// Currently, volume can be changed when player is running. Try manage this right after player starts.
String path = await flutterSound.startPlayer(null);
await flutterSound.setVolume(0.1);

Using the amplitude meter

The amplitude meter allows displaying a basic representation of the input sound. When enabled, it returns values ranging 0-120dB.

//// By default this option is disabled, you can enable it by calling
setDbLevelEnabled(true);
//// You can tweak the frequency of updates by calling this function (unit is seconds)
updateDbPeakProgress(0.8);
//// You need to subscribe in order to receive the value updates
_dbPeakSubscription = flutterSound.onRecorderDbPeakChanged.listen((value) {
  setState(() {
    this._dbLevel = value;
  });
});

TODO

  • Seeking example in Exmaple project
  • Volume Control
  • Sync timing for recorder callback handler

Help Maintenance

I've been maintaining quite many repos these days and burning out slowly. If you could help me cheer up, buying me a cup of coffee will make my life really happy and get much energy out of it.
Buy Me A Coffee

flutter_sound's People

Contributors

ajmal-m-a avatar edman avatar ened avatar haideraltahan avatar hyochan avatar jonids avatar justsoft avatar magicaltux avatar mansya avatar mike-gallego avatar

Watchers

 avatar

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.