-
Notifications
You must be signed in to change notification settings - Fork 203
feat: enhance waveform animation, extraction flexibility and state management #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
62e1ed3
07b9a8a
0b6cc37
4bd0646
5b4c0cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,8 @@ class AudioFileWaveforms extends StatefulWidget { | |
| /// If decoration is used then use color in it. | ||
| final Color? backgroundColor; | ||
|
|
||
| /// Duration for animation. Defaults to 500 milliseconds. | ||
| /// Duration for animation. If set to Duration.zero, no animation will occur. | ||
| /// Defaults to 500 milliseconds. | ||
| final Duration animationDuration; | ||
|
|
||
| /// Curve for animation. Defaults to Curves.easeIn | ||
|
|
@@ -144,18 +145,26 @@ class _AudioFileWaveformsState extends State<AudioFileWaveforms> | |
| void initState() { | ||
| super.initState(); | ||
| _initialiseVariables(); | ||
| _growingWaveController = AnimationController( | ||
| vsync: this, | ||
| duration: widget.animationDuration, | ||
| ); | ||
| _growAnimation = CurvedAnimation( | ||
| parent: _growingWaveController, | ||
| curve: widget.animationCurve, | ||
| ); | ||
|
|
||
| _growingWaveController | ||
| ..forward() | ||
| ..addListener(_updateGrowAnimationProgress); | ||
| if (widget.animationDuration == Duration.zero) { | ||
| _growAnimationProgress = 1.0; | ||
| } else { | ||
| _growingWaveController = AnimationController( | ||
| vsync: this, | ||
| duration: widget.animationDuration, | ||
| ); | ||
| _growAnimation = CurvedAnimation( | ||
| parent: _growingWaveController, | ||
| curve: widget.animationCurve, | ||
| ); | ||
|
|
||
| _growingWaveController | ||
| ..forward() | ||
| ..addListener(_updateGrowAnimationProgress); | ||
| } | ||
|
|
||
| _initializeAudioProgress(); | ||
|
|
||
| onCurrentDurationSubscription = | ||
| playerController.onCurrentDurationChanged.listen((event) { | ||
| _seekProgress.value = event; | ||
|
|
@@ -188,10 +197,50 @@ class _AudioFileWaveformsState extends State<AudioFileWaveforms> | |
| onCurrentExtractedWaveformData?.cancel(); | ||
| onCompletionSubscription.cancel(); | ||
| playerController.removeListener(_addWaveformDataFromController); | ||
| _growingWaveController.dispose(); | ||
|
|
||
| if (widget.animationDuration != Duration.zero) { | ||
| _growingWaveController.dispose(); | ||
| } | ||
|
|
||
| super.dispose(); | ||
| } | ||
|
|
||
| @override | ||
| void didUpdateWidget(AudioFileWaveforms oldWidget) { | ||
| super.didUpdateWidget(oldWidget); | ||
|
|
||
| // Vérifier si les waveformData ont changé | ||
| if (widget.waveformData != oldWidget.waveformData) { | ||
| if (widget.waveformData.isNotEmpty) { | ||
| _addWaveformData(widget.waveformData); | ||
| } | ||
| } | ||
|
|
||
| // Vérifier si d'autres propriétés importantes ont changé | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add comment in english There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I forgot. |
||
| if (widget.playerWaveStyle != oldWidget.playerWaveStyle || | ||
| widget.size != oldWidget.size || | ||
| widget.waveformType != oldWidget.waveformType) { | ||
| if (mounted) setState(() {}); | ||
| } | ||
|
|
||
| // Mettre à jour les variables si nécessaire | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well, add comment in english There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I forgot. |
||
| if (widget.margin != oldWidget.margin || | ||
| widget.padding != oldWidget.padding || | ||
| widget.decoration != oldWidget.decoration || | ||
| widget.backgroundColor != oldWidget.backgroundColor || | ||
| widget.animationDuration != oldWidget.animationDuration || | ||
| widget.animationCurve != oldWidget.animationCurve || | ||
| widget.clipBehavior != oldWidget.clipBehavior) { | ||
| margin = widget.margin; | ||
| padding = widget.padding; | ||
| decoration = widget.decoration; | ||
| backgroundColor = widget.backgroundColor; | ||
| animationDuration = widget.animationDuration; | ||
| animationCurve = widget.animationCurve; | ||
| clipBehavior = widget.clipBehavior; | ||
| } | ||
| } | ||
|
|
||
| double _audioProgress = 0.0; | ||
| double _cachedAudioProgress = 0.0; | ||
|
|
||
|
|
@@ -406,4 +455,16 @@ class _AudioFileWaveformsState extends State<AudioFileWaveforms> | |
| } | ||
| }); | ||
| } | ||
|
|
||
| Future<void> _initializeAudioProgress() async { | ||
| if (playerController.playerState == PlayerState.paused && | ||
| playerController.maxDuration > 0) { | ||
| final currentPosition = | ||
| await playerController.getDuration(DurationType.current); | ||
| if (currentPosition > 0) { | ||
| _seekProgress.value = currentPosition; | ||
| _updatePlayerPercent(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,7 +127,8 @@ class PlayerController extends ChangeNotifier { | |
| Future<void> preparePlayer({ | ||
| required String path, | ||
| double? volume, | ||
| bool shouldExtractWaveform = true, | ||
| WaveformExtractionType waveformExtractionType = | ||
| WaveformExtractionType.noExtraction, | ||
| int noOfSamples = 100, | ||
| }) async { | ||
| path = Uri.parse(path).path; | ||
|
|
@@ -143,20 +144,18 @@ class PlayerController extends ChangeNotifier { | |
| _setPlayerState(PlayerState.initialized); | ||
| } | ||
|
|
||
| if (shouldExtractWaveform) { | ||
| waveformExtraction | ||
| .extractWaveformData( | ||
| if (waveformExtractionType != WaveformExtractionType.noExtraction) { | ||
| var extraction = waveformExtraction.extractWaveformData( | ||
| path: path, | ||
| noOfSamples: noOfSamples, | ||
| ) | ||
| .then( | ||
| (value) { | ||
| )..then((values) { | ||
| waveformExtraction.waveformData | ||
| ..clear() | ||
| ..addAll(value); | ||
| notifyListeners(); | ||
| }, | ||
| ); | ||
| ..addAll(values); | ||
| }); | ||
| if (waveformExtractionType == WaveformExtractionType.extractSync) { | ||
| await extraction; | ||
| } | ||
|
Comment on lines
+147
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add some comment above this to explain the logic here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed the verifiable shouldExtractWaveform to enum, to allow for different cases. Don't extract waveforms, extract but don't wait for extraction to finish and finally extract and wait for extraction to finish. |
||
| } | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ class WaveformExtractionController { | |
|
|
||
| /// This returns waveform data which can be used by [AudioFileWaveforms] | ||
| /// to display waveforms. | ||
| List<double> get waveformData => _waveformData.toList(); | ||
| List<double> get waveformData => _waveformData; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't want to allow users to modify the list but if you had any reason to remove it then can please mention it here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed it because this part of the code you've written isn't working properly any more. |
||
|
|
||
| /// A stream to get current extracted waveform data. This stream will emit | ||
| /// list of doubles which are waveform data point. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| enum WaveformExtractionType { | ||
| /// No waveform extraction will be performed. | ||
| noExtraction, | ||
|
|
||
| /// Extract waveform data asynchronously without waiting for the result. | ||
| extractAsync, | ||
|
|
||
| /// Extract waveform data and wait until it's completed before continuing. | ||
| extractSync, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please explain why you're only disposing when animation duration isn't zero?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because the controller is not instantiated over the duration is set to zero