1

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>
Harsh Vardhan
  • 559
  • 1
  • 3
  • 12
  • First, try adding this attribute to your ``: `android:foregroundServiceType="mediaPlayback"`. If that alone doesn't work, try also disabling the system battery optimisation setting for your app (if that works, you can consider prompting the user to open that setting on first startup). – Ryan Heise Jul 18 '22 at 14:27
  • @RyanHeise thanks for the help I removed androidStopForegroundOnPause: true, from my main.dart and now it's working properly. – Harsh Vardhan Jul 19 '22 at 18:41
  • That shouldn't make any difference though, because `true` was the same as the default. Are you sure that's the only thing you changed from before? Otherwise that is impossible to explain. – Ryan Heise Jul 20 '22 at 02:18
  • @RyanHeise I don't know why but I have tested multiple times and it has worked fine till now. – Harsh Vardhan Jul 20 '22 at 17:38
  • I was asking you if there was anything else that you changed, because the thing that you mentioned will have no effect. – Ryan Heise Jul 21 '22 at 09:45

1 Answers1

-1

You should remove androidStopForegroundOnPause as this comment suggests.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    @Adriaan It may be OK this time because he's repeating an "it worked" comment by the author of the question as an answer so now we know what the answer is and there is no other answer posted. No explanation is indeed the problem. – karel Nov 21 '22 at 14:15
  • @karel barely though, because they also clearly state they have no clue why, making this a classic case of "Use this" without explanation. – Adriaan Nov 21 '22 at 14:16