skip to Main Content

My app (made with Flutter but this should not matter) has something like a timer functionality that makes a tick sound in regular periods (between 10s and 3min). I have the background mode Audio, AirPlay, and Picture in Picture activated and the following in my Info.plist.

    <key>UIBackgroundModes</key>
    <array>
        <string>audio</string>
    </array>

but the audio will still stop when running in background.

This occurs when running the app in profile mode, when I run in debug mode, the audio continues when running in background.

What can I do to have the audio continue to run in background?

3

Answers


  1. Well, unless you did this in native code (Swift/Objective-C), your code is running inside the Flutter engine – probably with some Dart Timer.periodic.

    The Flutter engine may be killed off at any point in time when the app is in the background. On Android this can even happen when simply switching to the camera and back to the app afterwards. On iOS usually after some fixed time or on high system load.

    In this regard, Flutter (and most other cross-platform toolkits) are very different to native apps.

    You can start with the official documentation here: https://flutter.dev/docs/development/packages-and-plugins/background-processes
    This may be a good article: https://medium.com/vrt-digital-studio/flutter-workmanager-81e0cfbd6f6e

    I don’t know enough about iOS but I think there is no easy way to schedule execution in the small intervals you require. On Android something like the AlarmManager can be used.

    You can try writing the scheduling code natively and schedule it from the app via a MethodChannel when the period is set.

    You can look at these libraries:
    https://pub.dev/packages/workmanager (probably can’t wake up at the small intervals you need)
    https://pub.dev/packages/android_alarm_manager_plus (only for Android)
    https://pub.dev/packages/audio_service (may give you some idea on how to achieve background execution on iOS)

    Edit:
    After reading more about Enabling Background Audio on iOS, it seems to me, that this only works when using
    an AVAudioSession. Which you are probably not using. To get this working you need some native code. The audio_service package uses such a session. You can try scheduling with Dart code and playing the sound via the audio_service package. Sounds like it could work but I have no experience with this package.

    Login or Signup to reply.
  2. There is a relevant note in the audio_service 0.18.0 README which can help here:

    Note that the audio background mode permits an app to run in the background only for the purpose of playing audio. The OS may kill your process if it sits idly without playing audio, for example, by using a timer to sleep for a few seconds. If your app needs to pause for a few seconds between audio tracks, consider playing a silent audio track to create that effect rather than using an idle timer.

    Login or Signup to reply.
  3. Please, pay attention to the answer from @RyanHeise – he’s correct on the point of using AudioSession: in background you should play either sound or silence. As soon as audio will be paused, app can be suspended.

    Also, Important Note: when app entering background, scheduled timers will pause. That’s why you might think it stopped working. Do not use scheduling via Timers on background – rely on events from the system.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search