Reputation: 2363
I am having a hard time with the logic here.
I build a media player in Flutter, and I am trying to get it to skip ahead 15 secs and go back 15 secs, calculated.
The media player calculates the following.
onDuration(), which gets the entire duration of the audio track in min and secs.
Then onPosition(), which calculates the position its on in percent to the audio file.
So for instance if the audio file is 10 secs long 1 secs in will be .1 in the onPosition() function.
I am sure there is some crazy math formula here that can be done to get the duration and position to go into a variable that will display the position in secs so that I can skip ahead +15 secs.
So far all that I tried, when I am outputting is is the decimal %
What is making it hard for me is the duration is coming in as min/secs while the position is a % so there are conversions that need to be done.
Upvotes: 1
Views: 1017
Reputation: 8427
A simple rule of three may help you in this situation:
Audio duration in seconds
/ 15 seconds
= 1
/ x
x
= 15 seconds
/ Audio duration in seconds
Translating to Dart:
double percentageOf15SecondsInDuration(Duration duration) => 15 / duration.inSeconds;
To forward 15 seconds, add the returned value to your actual position. Do the opposite to backward 15 seconds.
Upvotes: 2