Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:audio_wave/audio_wave.dart';
import 'dart:ui' as ui show Gradient;

void main() => runApp(const MyApp());

Expand Down Expand Up @@ -50,12 +51,32 @@ class _HomeState extends State<Home> {
children: [
const SizedBox(height: 200),
AudioWave(
size: Size(MediaQuery.of(context).size.width, 200.0),
size: Size(MediaQuery.of(context).size.width, 100.0),
updateFrequency: updateFrequency,
waveController: waveController,
margin: const EdgeInsets.all(10.0),
margin: const EdgeInsets.all(20.0),
waveStyle: WaveStyle(
waveColor: Colors.white,
middleLineColor: Colors.white,
durationLinesColor: Colors.white,
durationLinesHeight: 8.0,
extendWaveform: true,
showMiddleLine: false,
labelSpacing: 8.0,
durationStyle: const TextStyle(
color: Colors.white,
fontSize: 18.0,
),
gradient: ui.Gradient.linear(
const Offset(70, 50),
Offset(MediaQuery.of(context).size.width / 2, 0),
[Colors.red, Colors.green],
),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14.0), color: Colors.black),
borderRadius: BorderRadius.circular(14.0),
color: Colors.black
),
),
const SizedBox(height: 40),
Row(
Expand Down
3 changes: 2 additions & 1 deletion lib/audio_wave.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ library audio_wave;

export 'src/audio_wave.dart';
export 'src/base/wave_style.dart';
export 'src/base/wave_controller.dart';
export 'src/base/wave_controller.dart';
export 'src/base/utils.dart';
55 changes: 28 additions & 27 deletions lib/src/audio_wave.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,33 @@ class _AudioWaveState extends State<AudioWave> {
child: CustomPaint(
size: widget.size,
painter: WavePainter(
waveThickness: widget.waveStyle.waveThickness,
middleLineThickness: widget.waveStyle.middleLineThickness,
middleLineColor: widget.waveStyle.middleLineColor,
waveData: widget.waveController.waveData,
callPushback: widget.waveController.shouldRefresh,
bottomPadding:
widget.waveStyle.bottomPadding ?? widget.size.height / 2,
spacing: widget.waveStyle.spacing,
waveCap: widget.waveStyle.waveCap,
showBottom: widget.waveStyle.showBottom,
showTop: widget.waveStyle.showTop,
waveColor: widget.waveStyle.waveColor,
showMiddleLine: widget.waveStyle.showMiddleLine,
totalBackDistance: _totalBackDistance,
dragOffset: _dragOffset,
pushBack: _pushBackWave,
initialPosition: _initialPosition,
extendWaveform: widget.waveStyle.extendWaveform,
showHourInDuration: widget.waveStyle.showHourInDuration,
showDurationLine: widget.waveStyle.showDurationLine,
durationLinesColor: widget.waveStyle.durationLinesColor,
durationStyle: widget.waveStyle.durationStyle,
updateFrequecy: const Duration(seconds: 1).inMilliseconds /
widget.waveController.updateFrequency.inMilliseconds,
durationTextPadding: widget.waveStyle.durationTextPadding,
durationLinesHeight: widget.waveStyle.durationLinesHeight,
waveThickness: widget.waveStyle.waveThickness,
middleLineThickness: widget.waveStyle.middleLineThickness,
middleLineColor: widget.waveStyle.middleLineColor,
waveData: widget.waveController.waveData,
callPushback: widget.waveController.shouldRefresh,
bottomPadding:
widget.waveStyle.bottomPadding ?? widget.size.height / 2,
spacing: widget.waveStyle.spacing,
waveCap: widget.waveStyle.waveCap,
showBottom: widget.waveStyle.showBottom,
showTop: widget.waveStyle.showTop,
waveColor: widget.waveStyle.waveColor,
showMiddleLine: widget.waveStyle.showMiddleLine,
totalBackDistance: _totalBackDistance,
dragOffset: _dragOffset,
pushBack: _pushBackWave,
initialPosition: _initialPosition,
extendWaveform: widget.waveStyle.extendWaveform,
showHourInDuration: widget.waveStyle.showHourInDuration,
showDurationLine: widget.waveStyle.showDurationLine,
durationLinesColor: widget.waveStyle.durationLinesColor,
durationStyle: widget.waveStyle.durationStyle,
updateFrequecy: const Duration(seconds: 1).inMilliseconds /
widget.waveController.updateFrequency.inMilliseconds,
durationTextPadding: widget.waveStyle.durationTextPadding,durationLinesHeight: widget.waveStyle.durationLinesHeight,
labelSpacing: widget.waveStyle.labelSpacing,
gradient: widget.waveStyle.gradient,
),
),
),
Expand Down Expand Up @@ -145,7 +145,8 @@ class _AudioWaveState extends State<AudioWave> {
}

///This will handle pushing back the wave when it reaches to middle/end of the
///given size.width
///given size.width.
///
///This will also handle refreshing the wave after scrolled
void _pushBackWave() {
if (_isScrolled) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/base/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ extension IntExtension on int {

///state of recorer
enum RecorderState { initialized, recording, paused, stopped }

36 changes: 27 additions & 9 deletions lib/src/base/wave_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ class WaveController extends ChangeNotifier {

bool _hasPermission = false;

///If we have microphone permission or not.
bool get hasPermission => _hasPermission;

///Use this to check permission and starts recording.
///
///Can be called after pausing.
///If called after stoping the recording, it will re-initialize
///If called after stoping the recording, it will re-initialize.
///
///Path paramater is optional, if you want to provide provide with name
///of the file.
///
///eg. /dir1/dir2/file-name
///
///Only supported audio file format is .m4a for IOS and .mp3 for Android.
///More formats will be added shortly.
Future<void> record([String? path]) async {
if (_recorderState != RecorderState.recording) {
await _checkPermission();
await checkPermission();
if (_hasPermission) {
if (Platform.isAndroid && _recorderState == RecorderState.stopped) {
await _initRecorder(path);
Expand Down Expand Up @@ -89,17 +101,22 @@ class WaveController extends ChangeNotifier {
notifyListeners();
}

///checks microphone permission
Future<void> _checkPermission() async {
///This method can be used to check microphone permission.
/// Returns true if we have permission
///else false.
///
/// This method is called during record().
Future<bool> checkPermission() async {
final result = await AudioWaveInterface.instance.checkPermission();
if (result) {
_hasPermission = result;
}
notifyListeners();
return _hasPermission;
}

///Use this to pause recording.
///Can start recording again after pausing
///Can start recording again after pausing.
Future<void> pause() async {
if (_recorderState == RecorderState.recording) {
_isRecording = (await AudioWaveInterface.instance.pause()) ?? true;
Expand All @@ -113,7 +130,7 @@ class WaveController extends ChangeNotifier {
}

///Use this stop recording.
///Resouces are freed after calling this.
///Resouces are freed after calling this and file is saved.
Future<void> stop() async {
if (_recorderState == RecorderState.recording ||
_recorderState == RecorderState.paused) {
Expand Down Expand Up @@ -165,20 +182,21 @@ class WaveController extends ChangeNotifier {
}

///Use this function to get wave to the initial state after scrolling,
///whether recording is stopped or running
///whether recording is stopped or running.
void refresh() {
_shouldRefresh = true;
notifyListeners();
}

///This function can be used to handle the refresh state.
///for most cases refresh() should be fine
///for most cases refresh() should be fine.
void setRefresh(bool refresh) {
_shouldRefresh = refresh;
notifyListeners();
}

///Call this to free [resouces], it will also dispose the controller
///This function must be called to free [resouces],
///it will also dispose the controller.
void disposeFunc() async {
if (_timer != null) {
_timer!.cancel();
Expand Down
8 changes: 8 additions & 0 deletions lib/src/base/wave_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ class WaveStyle {
final double labelSpacing;

///It might happen that label text gets cut or have extra clipping.
///
///So use this to add or remove clipping
final double? extraClipperHeight;

///Value > 0 will be padded right and value < 0 will be padded left
final double durationTextPadding;

///Provide gradient to waveform using this. Use shader as shown in example.
///
///For only two colors. offset.dx is gradient effect starts and offset.dy
///is for y-axis effect.
final Shader? gradient;

const WaveStyle({
this.waveColor = Colors.blueGrey,
this.showMiddleLine = true,
Expand All @@ -87,6 +94,7 @@ class WaveStyle {
this.labelSpacing = 16.0,
this.durationTextPadding = 20.0,
this.durationLinesColor = Colors.blueAccent,
this.gradient,
}) : assert(waveThickness < spacing,
"waveThickness can't be greater than spacing");
}
12 changes: 10 additions & 2 deletions lib/src/wave_painter.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:audio_wave/src/base/label.dart';
import 'package:flutter/material.dart';

import 'base/utils.dart';

///This will paint the waveform
///Addtion Information
///Addtional Information
///this gives location of first wave from right to left
///-totalBackDistance.dx + dragOffset.dx + (spacing * i)
///this gives location of first wave from left to right
Expand Down Expand Up @@ -38,6 +37,7 @@ class WavePainter extends CustomPainter {
final double durationTextPadding;
final double durationLinesHeight;
final double labelSpacing;
final Shader? gradient;

WavePainter({
required this.waveData,
Expand Down Expand Up @@ -65,6 +65,7 @@ class WavePainter extends CustomPainter {
required this.durationTextPadding,
required this.durationLinesHeight,
required this.labelSpacing,
required this.gradient,
}) : _wavePaint = Paint()
..color = waveColor
..strokeWidth = waveThickness
Expand All @@ -82,6 +83,9 @@ class WavePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
for (var i = 0; i < waveData.length; i++) {
///wave gradient
if (gradient != null) _waveGradient();

if (((spacing * i) + dragOffset.dx + spacing >
size.width / (extendWaveform ? 1 : 2) + totalBackDistance.dx) &&
callPushback) {
Expand Down Expand Up @@ -197,4 +201,8 @@ class WavePainter extends CustomPainter {
waveData[i] + size.height - bottomPadding),
_wavePaint);
}

void _waveGradient() {
_wavePaint.shader = gradient;
}
}
38 changes: 0 additions & 38 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,12 @@ dev_dependencies:
sdk: flutter
flutter_lints: ^1.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:
# This section identifies this Flutter project as a plugin project.
# The 'pluginClass' and Android 'package' identifiers should not ordinarily
# be modified. They are used by the tooling to maintain consistency when
# adding or updating assets for this project.
plugin:
platforms:
android:
package: com.simform.audiowave.audio_wave
pluginClass: AudioWavePlugin
ios:
pluginClass: AudioWavePlugin

# To add assets to your plugin package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

# To add custom fonts to your plugin package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages