Skip to content

Commit

Permalink
Added bitrate selection (SimformSolutionsPvtLtd#84)
Browse files Browse the repository at this point in the history
* Added bitrate selection
  • Loading branch information
abhay-s-rawat authored Sep 8, 2022
1 parent 852b5e7 commit 6a72018
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.1.5] - 7 Sept, 2022

* Added bitRate feature for Android & IOS (Default to 64kbps).

## [0.1.4] - 1 Aug, 2022

- Fixed [#71](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/71) - Bump compileSdkVersion/gradle/kotlin to match flutter 3.0 - thanks [@yohom](https://github.com/yohom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener {
recorder: MediaRecorder?,
encoder: Int,
outputFormat: Int,
sampleRate: Int
sampleRate: Int,
bitRate: Int
) {
recorder?.apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(getOutputFormat(outputFormat))
setAudioEncoder(getEncoder(encoder))
setAudioSamplingRate(sampleRate)
setAudioEncodingBitRate(bitRate)
setOutputFile(path)
try {
recorder.prepare()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private var encoder: Int = 0
private var outputFormat: Int = 0
private var sampleRate: Int = 16000
private var bitRate: Int = 64000
private lateinit var applicationContext: Context

//Todo: bitrate
Expand All @@ -51,7 +52,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
encoder = (call.argument(Constants.encoder) as Int?) ?: 0
outputFormat = (call.argument(Constants.outputFormat) as Int?) ?: 0
sampleRate = (call.argument(Constants.sampleRate) as Int?) ?: 16000
checkPathAndInitialiseRecorder(result, encoder, outputFormat, sampleRate)
bitRate = (call.argument(Constants.bitRate) as Int?) ?: 64000
checkPathAndInitialiseRecorder(result, encoder, outputFormat, sampleRate, bitRate)
}
Constants.startRecording -> audioRecorder.startRecorder(result, recorder)
Constants.stopRecording -> {
Expand Down Expand Up @@ -153,7 +155,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
result: Result,
encoder: Int,
outputFormat: Int,
sampleRate: Int
sampleRate: Int,
bitRate: Int
) {
try {
recorder = MediaRecorder()
Expand All @@ -174,7 +177,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
recorder,
encoder,
outputFormat,
sampleRate
sampleRate,
bitRate
)
} catch (e: IOException) {
Log.e(Constants.LOG_TAG, "Failed to create file")
Expand All @@ -186,7 +190,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
recorder,
encoder,
outputFormat,
sampleRate
sampleRate,
bitRate
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object Constants {
const val encoder = "encoder"
const val outputFormat = "outputFormat"
const val sampleRate = "sampleRate"
const val bitRate = "bitRate"
const val fileNameFormat = "dd-MM-yy-hh-mm-ss"


Expand Down
9 changes: 7 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class _HomeState extends State<Home> with WidgetsBindingObserver {
..androidEncoder = AndroidEncoder.aac
..androidOutputFormat = AndroidOutputFormat.mpeg4
..iosEncoder = IosEncoder.kAudioFormatMPEG4AAC
..sampleRate = 16000;
..sampleRate = 16000
..bitRate = 64000;
playerController1 = PlayerController()
..addListener(() {
if (mounted) setState(() {});
Expand Down Expand Up @@ -306,7 +307,11 @@ class _HomeState extends State<Home> with WidgetsBindingObserver {
if (isRecording) {
recorderController.reset();
final path = await recorderController.stop(false);
if (path != null) await playerController5.preparePlayer(path);

if (path != null) {
debugPrint("Recorded file size: ${File(path).lengthSync()}");
await playerController5.preparePlayer(path);
}
} else {
await recorderController.record(path);
}
Expand Down
3 changes: 2 additions & 1 deletion ios/Classes/AudioRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ public class AudioRecorder: NSObject, AVAudioRecorderDelegate{
var hasPermission: Bool = false
public var meteringLevels: [Float]?

public func startRecording(_ result: @escaping FlutterResult,_ path: String?,_ encoder : Int?,_ sampleRate : Int?,_ fileNameFormat: String){
public func startRecording(_ result: @escaping FlutterResult,_ path: String?,_ encoder : Int?,_ sampleRate : Int?,_ bitRate : Int?,_ fileNameFormat: String){
let settings = [
AVEncoderBitRateKey: bitRate ?? 64000,
AVFormatIDKey: getEncoder(encoder ?? 0),
AVSampleRateKey: sampleRate ?? 16000,
AVNumberOfChannelsKey: 1,
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/SwiftAudioWaveformsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SwiftAudioWaveformsPlugin: NSObject, FlutterPlugin {
switch call.method {
case Constants.startRecording:
audioRecorder.startRecording(result, args?[Constants.path] as? String,
args?[Constants.encoder] as? Int, args?[Constants.sampleRate] as? Int,Constants.fileNameFormat)
args?[Constants.encoder] as? Int, args?[Constants.sampleRate] as? Int, args?[Constants.bitRate] as? Int,Constants.fileNameFormat)
break
case Constants.pauseRecording:
audioRecorder.pauseRecording(result)
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct Constants {
static let path = "path"
static let encoder = "encoder"
static let sampleRate = "sampleRate"
static let bitRate = "bitRate"
static let fileNameFormat = "YY-MM-dd-HH-mm-ss"
static let resumeRecording = "resumeRecording"

Expand Down
9 changes: 6 additions & 3 deletions lib/src/base/audio_waveforms_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ class AudioWaveformsInterface {
MethodChannel(Constants.methodChannelName);

///platform call to start recording
Future<bool> record(int audioFormat, int sampleRate, [String? path]) async {
Future<bool> record(int audioFormat, int sampleRate, int bitRate,
[String? path]) async {
final isRecording = await _methodChannel.invokeMethod(
Constants.startRecording,
Platform.isIOS
? {
Constants.path: path,
Constants.encoder: audioFormat,
Constants.sampleRate: sampleRate,
Constants.bitRate: bitRate,
}
: null);
return isRecording ?? false;
}

///platform call to initialise the recorder.
///This method is only required for Android platform
Future<bool> initRecorder(
String? path, int encoder, int outputFormat, int sampleRate) async {
Future<bool> initRecorder(String? path, int encoder, int outputFormat,
int sampleRate, int bitRate) async {
final initialized = await _methodChannel.invokeMethod(
Constants.initRecorder,
{
Constants.path: path,
Constants.outputFormat: outputFormat,
Constants.encoder: encoder,
Constants.sampleRate: sampleRate,
Constants.bitRate: bitRate,
},
);
return initialized ?? false;
Expand Down
1 change: 1 addition & 0 deletions lib/src/base/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Constants {
static const String encoder = 'encoder';
static const String outputFormat = 'outputFormat';
static const String sampleRate = 'sampleRate';
static const String bitRate = 'bitRate';
static const String readAudioFile = 'readAudioFile';
static const String convertToBytes = 'convertToBytes';
static const String preparePlayer = "preparePlayer";
Expand Down
9 changes: 8 additions & 1 deletion lib/src/controllers/recorder_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class RecorderController extends ChangeNotifier {

late int sampleRate = 16000;

late int bitRate = 64000;

///Db we get from native is too high so in Android it the value is subtracted
///and in IOS value added
late double normalizationFactor = Platform.isAndroid ? 60 : 40;
Expand Down Expand Up @@ -105,6 +107,7 @@ class RecorderController extends ChangeNotifier {
_isRecording = await AudioWaveformsInterface.instance.record(
Platform.isIOS ? iosEncoder.index : androidEncoder.index,
sampleRate,
bitRate,
path);
if (_isRecording) {
_recorderState = RecorderState.recording;
Expand All @@ -124,7 +127,11 @@ class RecorderController extends ChangeNotifier {
///This method is only required for Android platform
Future<void> _initRecorder(String? path) async {
final initialized = await AudioWaveformsInterface.instance.initRecorder(
path, androidEncoder.index, androidOutputFormat.index, sampleRate);
path,
androidEncoder.index,
androidOutputFormat.index,
sampleRate,
bitRate);
if (initialized) {
_recorderState = RecorderState.initialized;
} else {
Expand Down

0 comments on commit 6a72018

Please sign in to comment.