Search code examples
androidandroid-workmanagerbackground-service

Is work manager suitable for playing music in background?


I'm trying to playing music in background and before I used Service for this goal, But now I want to know, Can I use work manager instead of service for playing music? Can anyone show me a guideline?


Solution

  • From the docs

    For work that is deferrable and expected to run even if your device or application restarts, use WorkManager. WorkManager is an Android library that gracefully runs deferrable background work when the work's conditions (like network availability and power) are satisfied.

    In your case, I don't think your work is deferrable. You want to start the playing as soon as the user clicks on the track. Work manager are more suitable for things which you want to perform at some point in the future based on some conditions like uploading logs or sending batch events etc. If I were you I would go with normal(foreground) service.

    You can find the docs here: https://developer.android.com/guide/background