Skip to content
Open
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
51 changes: 35 additions & 16 deletions lib/audio/types/ringtone_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class RingtonePlayer {
await _play(ringtoneUri, vibrate: vibrate, loopMode: LoopMode.one);
}



static Future<void> playAlarm(Alarm alarm,
{LoopMode loopMode = LoopMode.one}) async {
await activePlayer?.stop();
Expand Down Expand Up @@ -81,7 +79,6 @@ class RingtonePlayer {

static Future<void> setVolume(double volume) async {
logger.t("Setting volume to $volume");
_stopRisingVolume = true;
await activePlayer?.setVolume(volume);
}

Expand All @@ -95,8 +92,6 @@ class RingtonePlayer {
// double duration = double.infinity,
}) async {
try {
_stopRisingVolume = false;

RingtoneManager.lastPlayedRingtoneUri = ringtoneUri;
if (_vibratorIsAvailable && vibrate) {
Vibration.vibrate(pattern: [500, 1000], repeat: 0);
Expand All @@ -113,20 +108,15 @@ class RingtonePlayer {
logger.t("Starting at random position: $randomNumber");
activePlayer?.seek(duration * randomNumber);
}
await setVolume(volume);

// Gradually increase the volume
if (secondsToMaxVolume > 0) {
for (int i = 0; i <= 10; i++) {
Future.delayed(
Duration(milliseconds: i * (secondsToMaxVolume * 100)),
() {
if (!_stopRisingVolume) {
setVolume((i / 10) * volume);
}
},
);
}
_scheduleVolumeIncrease(
maxVolume: volume,
secondsToMaxVolume: secondsToMaxVolume,
);
} else {
await setVolume(volume);
}
// Future.delayed(
// Duration(seconds: duration.toInt()),
Expand Down Expand Up @@ -157,6 +147,35 @@ class RingtonePlayer {
await Vibration.cancel();
}
RingtoneManager.lastPlayedRingtoneUri = "";
_stopRisingVolume = true;
}

static void _scheduleVolumeIncrease({
required int secondsToMaxVolume,
required double maxVolume,
}) {
_stopRisingVolume = false;

Future(() async {
final startTime = DateTime.now();

for (int i = 0; i <= 10; i++) {
if (_stopRisingVolume) {
return;
}

await setVolume((i / 10) * maxVolume);

final timeUntilNextIncrease = i * (secondsToMaxVolume * 100);
while (DateTime.now().difference(startTime).inMilliseconds <
timeUntilNextIncrease) {
await Future.delayed(const Duration(milliseconds: 10));

if (_stopRisingVolume) {
return;
}
}
}
});
}
}