florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
757 stars 370 forks source link

Notifications dismisses on app close despite playInBackground enabled #150

Closed rahulserver closed 4 years ago

rahulserver commented 4 years ago

Flutter Version

My version : Flutter 1.19.0-2.0.pre.173 • channel master • https://github.com/flutter/flutter.git

Lib Version

My version : assets_audio_player: ^1.6.3 assets_audio_player_web: ^1.6.3

Platform (Android / iOS / web) + version

Platform : Android

Describe the bug

I am playing audio using the plugin like this:

AssetsAudioPlayer assetsAudioPlayer = AssetsAudioPlayer();
// ......
    assetsAudioPlayer.open(
      myAudio,
      seek: Duration(seconds: mySegment.startSec),
      autoStart: true,
      respectSilentMode: true,
      showNotification: true,
      notificationSettings: NotificationSettings(
        customNextAction: (player) {
          next();
          print("next clicked");
          playSegmentHandler();
        },
        customPrevAction: (player) {
          previous();
          print("prevClicked");
          playSegmentHandler();
        },
        stopEnabled: false,
      ),
      playInBackground: PlayInBackground.enabled,
    );

This causes the notification to be shown when app is running(even if screen closes). But as soon as app exits, the notification disappears. I am not sure if this is the intended behavior and is there a way to make the notification stay as a foreground service even if I exit the app(like google play music app).

florent37 commented 4 years ago

Yes it's an issue, with ''enabled'' you should keep the notification in background

florent37 commented 4 years ago

when you say But as soon as app exits how do you exit ? pressing back or home ?

florent37 commented 4 years ago

because if you press back, it close the app, so the notif too (it's related to the app' process, which is closes on back)

rahulserver commented 4 years ago

@florent37 pressing home does not dismiss the notification. But pressing back does.

Ideally I would expect the notification to stick there as a foreground service(as happens in other popular android apps). Doesn't Just having the notification when user is already running the app defeat the purpose of showing a notification?

florent37 commented 4 years ago

I only implemented the foreground service, because a part of the library is implemented in flutter (ex: playlist, custom notif actions etc) flutter code can only be executed when the app is in foreground (or attached to a foreground service)

if you press home to "exit" the app, your player is not visible but you can continue using your app, it's the same no ?

florent37 commented 4 years ago

I cannot because of technical stack (flutter) implement a background service that handle all the library

rahulserver commented 4 years ago

I only implemented the foreground service, because a part of the library is implemented in flutter (ex: playlist, custom notif actions etc) flutter code can only be executed when the app is in foreground (or attached to a foreground service)

if you press home to "exit" the app, your player is not visible but you can continue using your app, it's the same no ?

This is not the normal way people would exit the app. Pressing the back button is a more common way. I can't tell my users to use "Home" to exit the app.

florent37 commented 4 years ago

It's an open source library, Feel free to fork it, try to implement a background service that handle all existing possibilities staying compatible with flutter & custom notif actions

I'll be happy to review it and merge it :)

but I don't think it's technically possible to do this

rahulserver commented 4 years ago

Probably this might be handled at application level using Isolate threads or something that allows the app to keep running despite the back button. I'd be glad to keep you updated if I find a solution.

This is the only audio plugin that I found working in whole of flutter packages. So thanks for the great work!

florent37 commented 4 years ago

You'll have to update the flutter initialisation to not be attached to an activity but into the application

I don't know how it can be done but maybe it's possible :/

RomanSoviak commented 1 month ago

The audio is playing after closing the app, but the notification became hidden. Does the lib support showing notification when app is in the closed state? @florent37