Basically the question, I have implemented Audio service properly as its working on devices android 11 or less. But on android 12 at around 10mins It stops working as the audio playback stops the background notification is automatically removed. My app plays music in the background so I need audio service to run in background until removed by user. Here is my manifest.

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.companyname.app">
      <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WAKE_LOCK"/>
        <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
        <uses-permission android:name="com.android.vending.BILLING" />

       <application
            android:label="app"
            android:name="${applicationName}"
            android:requestLegacyExternalStorage ="true"
            android:icon="@mipmap/ic_launcher">
            <activity
                android:name="com.ryanheise.audioservice.AudioServiceActivity" 
                android:exported="true"
                android:launchMode="singleTop"
                android:theme="@style/LaunchTheme"
                android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
                android:hardwareAccelerated="true"
                android:windowSoftInputMode="adjustResize">
                <!-- Specifies an Android theme to apply to this Activity as soon as
                     the Android process has started. This theme is visible to the user
                     while the Flutter UI initializes. After that, this theme continues
                     to determine the Window background behind the Flutter UI. -->
                <meta-data
                  android:name="io.flutter.embedding.android.NormalTheme"
                  android:resource="@style/NormalTheme"
                  />
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
            <activity
              android:name="com.yalantis.ucrop.UCropActivity"
              android:screenOrientation="portrait"
              android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
              <service android:name="com.ryanheise.audioservice.AudioService"
                android:exported="true">
              <intent-filter>
                <action android:name="android.media.browse.MediaBrowserService" />
              </intent-filter>
            </service>

            <!-- ADD THIS "RECEIVER" element -->
            <receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"
                android:exported="true" >
              <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
              </intent-filter>
            </receiver> 
                    <!-- Don't delete the meta-data below.
                 This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
            <meta-data
            android:name="com.google.mlkit.vision.DEPENDENCIES"
            android:value="ocr" />
            <meta-data
                android:name="flutterEmbedding"
                android:value="2" />
        </application>
    </manifest>


Solution 1: Manish Rajak

You should remove androidStopForegroundOnPause as this comment suggests.