I am currently working on a Flutter project and utilizing the circular_countdown_timer package to display a countdown timer. However, I am facing a challenge in implementing a feature where I want to play a "Tic" audio during the last 5 seconds of the countdown. Specifically, I would like to play a "Tic" audio for each last second of the countdown, summing up to a total of 5 "Tic" sounds.
I am seeking guidance on how to achieve this functionality within my Flutter application. How can I trigger the audio playback during the last 5 seconds of the countdown, ensuring that the "Tic" audio is played for each last second that counts down?
Any insights, suggestions, or code samples related to integrating audio playback during the countdown using the circular_countdown_timer package would be highly appreciated.
Thank you in advance for your support and expertise in resolving this challenge.
Widget:
/// Returns Body as per mode or mode conditions
Widget returnSmQuizBodyWidget({required BuildContext context}) {
if (_soloModeLogic.currentMode == 'Exclusive'{
return const SMWinScreen();
} else {
return Column(
children: [
//----------------------- Question Container
Stack(
clipBehavior: Clip.none,
children: [
//----------------------- Question Frame
returnQuestionFrame(context: context),
//----------------------- Timer
Positioned(
right: 0,
left: 0,
bottom: -25,
child: CircularCountDownTimer(
duration: _soloModeLogic.quizTime,
initialDuration: 0,
controller: _soloModeLogic.countDownController,
width: 5.w,
height: 7.h,
ringColor: Colors.grey.shade400,
ringGradient: null,
fillColor: _soloModeLogic.isFreezeColor
? Colors.lightBlueAccent
: Colors.red,
fillGradient: null,
backgroundColor: const Color(0xfff5f5f5),
backgroundGradient: null,
strokeWidth: 4.w,
strokeCap: StrokeCap.butt,
textStyle: TextStyle(
fontSize: 20.sp,
color: _soloModeLogic.isFreezeColor
? Colors.lightBlueAccent
: Colors.red,
fontWeight: FontWeight.bold,
),
textFormat: CountdownTextFormat.S,
isReverse: true,
isReverseAnimation: false,
isTimerTextShown: true,
autoStart: true,
onStart: () {},
onChange: (v) {},
onComplete: () =>
_soloModeLogic.onTimeOut(context: context),
),
),
],
),
],
);
}
}
2
Answers
I have created a demo app.
based on your code.
It does what you need.
One important thing I came to realize is that during the last five seconds, instead of playing/doing pause each second, which causes sync loss between counter and audio player, start playing audio from the last 5th second and stop at or before zero and select suitable audio file that takes appx 1 second to play a sound.
You can change the audio speed to adjust and you can also look out for alternate audio player library.
You can do anything with the code.(Fork/copy-pase,etc)
Thanks 🙂
Edit :
I realized that in future, I may delete the app from the repo, hence pasting important parts of the code here.
I have
assets/tick.mp3
file in the root directory.This is my
pubspec.yaml
file.This is my main.yaml file.
When it comes to audio handling in Flutter, I always go for the just_audio package. It does what’s needed with ease.
Your
CircularCountDownTimer
has anonChange
callback that should be sufficient for your use-case of playing a sound every second, in the last 5 seconds.Step 1
Install the
just_audio
package from here: https://pub.dev/packages/just_audioStep 2
In your
onChange
callback, you should be able to get the current duration (e.g. 23) and compare it to the final value (e.g. 0 if counting down), and if the difference is less than or equal to 4, play an audio. Since I’m not a 100% sure about the working of the countdown timer package, I will use pseudo-code.I hope this made sense. Feel free to ask anything if you’re not sure.
Once again, I’m not sure about how the
currentDuration
will be accessed as I have never used thecircular_countdown_timer
package.