5

So i've created an app that plays sounds after intervals - for my cycling and running routines.

But when i lock the screen of my android phone the app stops working after about 15 seconds.

The app's code look like so:

DateTime duration = new DateTime.fromMicrosecondsSinceEpoch(interval.inMicroseconds);
Timer counterSeconds;
Icon iconTimerStarter = new Icon(iconStart);
DateFormat minutesSeconds = new DateFormat("ms");
static AudioCache player = new AudioCache();

void startTimer(interval) {
  if (duration.millisecondsSinceEpoch == 0) {
    duration = new DateTime.fromMicrosecondsSinceEpoch(interval.inMicroseconds);
  }
  counterSeconds = new Timer.periodic(oneSec, (Timer t) => handleTick());
  _setIconForButton(new Icon(iconCancel));
}

void handleTick() {
  print(duration);
  setState(() {
    duration = duration.subtract(oneSec);
    if (duration.millisecondsSinceEpoch == 0) {
      stopTimer();
      if(sprint){
        player.play(alarmWalk);
        startTimer(Duration(seconds: 90));
      }else{
        startTimer(Duration(seconds: 30));
        player.play(alarmSprint);
      }
      sprint = !sprint;
    }
  });
}

The packages i use:

import 'package:audioplayers/audio_cache.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

I want the app to keep counting and playing sounds while the screen on my phone is locked. (it works fine when minimized but when the screen is locked it just freezes after like 15 seconds).

miyav miyav
  • 338
  • 1
  • 5
  • 18

1 Answers1

0

You can try using wakelock plugin to enabled/disable wakelock and see how it can fit your use case. I suggest visiting this similar Stack Overflow post for more details https://stackoverflow.com/a/66872228

Omatt
  • 8,564
  • 2
  • 42
  • 144