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 363 forks source link

ForegroundServiceStartNotAllowedException #719

Open Ilyosxon2721 opened 2 years ago

Ilyosxon2721 commented 2 years ago

3.0.5

My version : 3.0.5

Lib Version

My version :

Android

Platform :

Describe the bug

android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.imusic.host/com.github.florent37.assets_audio_player.notification.NotificationService

Small code to reproduce

audioPlayer.open(
  Playlist(audios: audioList, startIndex: index),
  loopMode: LoopMode.playlist,
  showNotification: true,
  autoStart: true,
  playInBackground: PlayInBackground.enabled,
  headPhoneStrategy: HeadPhoneStrategy.pauseOnUnplug,
  audioFocusStrategy: const AudioFocusStrategy.request(
    resumeAfterInterruption: true,
    resumeOthersPlayersAfterDone: true,
  ),
  notificationSettings: NotificationSettings(
    stopEnabled: false,
    customPlayPauseAction: (player) {
      if (audioPlayer.isPlaying.value == true) {
        print('True: ${audioPlayer.isPlaying.value}');
        audioPlayer.pause();
        changeIsPlaying(false);
      } else if (audioPlayer.isPlaying.value == false) {
        print('False: ${audioPlayer.isPlaying.value}');
        audioPlayer.play();
        changeIsPlaying(true);
      }
    },
    customStopAction: (player) {
      audioPlayer.stop();
      audioPlayer.pause();
      changeIsPlaying(false);
    },
  ),
);
ZhangZhiH commented 2 years ago

I met some equipment resumeOthersPlayersAfterDone android = true not to take effect

samatzp commented 1 year ago

Any solution?

akarapetsas commented 1 year ago

Add in your manifest the following permissions:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

Also add this service in your manifest too:

<service
        tools:node="replace"
        android:name=".notification.NotificationService"
        android:foregroundServiceType="mediaPlayback"
        android:enabled="true"
        android:exported="true">
</service>
Sanam-Gurung commented 1 year ago

Add in your manifest the following permissions:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

Also add this service in your manifest too:

<service
        tools:node="replace"
        android:name=".notification.NotificationService"
        android:foregroundServiceType="mediaPlayback"
        android:enabled="true"
        android:exported="true">
</service>

Done this but still facing the issue.

kalismeras61 commented 1 year ago

What is your android version ?

Sanam-Gurung commented 1 year ago

What is your android version ?

android 12, but users with android 12+ are facing this issue repeatedly.