ryanheise / just_audio

Audio Player
1.04k stars 659 forks source link

Android notification pause button not working as expected #978

Closed Devansh8086 closed 3 weeks ago

Devansh8086 commented 1 year ago

Which API doesn't behave as documented, and how does it misbehave? When using the notification pause button on Android the music stops after few minutes and app is getting crashed/closed automatically but this is not happening in all the devices (perfectly working in Samsung M12 - Android 13, moto e13 - Android 13).

Minimal reproduction project Provide a link here using one of two options: The example

To Reproduce (i.e. user steps, not code) Steps to reproduce the behavior:

  1. Launch the app
  2. Click Play
  3. Open the notification shade
  4. Click Pause on the notification
  5. App is getting crashed/closed automatically

Error messages

E/AndroidRuntime(28901): android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{729c672 u0 com.example.audio_demo/com.dooboolab.TauEngine.FlautoBackgroundAudioService}
E/AndroidRuntime(28901):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2126)
E/AndroidRuntime(28901):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(28901):    at android.os.Looper.loop(Looper.java:236)
E/AndroidRuntime(28901):    at android.app.ActivityThread.main(ActivityThread.java:8057)
E/AndroidRuntime(28901):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(28901):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
E/AndroidRuntime(28901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
I/Process (28901): Sending signal. PID: 28901 SIG: 9
Lost connection to device.

Expected behavior Music must stop.

Screenshots

Desktop (please complete the following information):

Smartphone (please complete the following information):

Flutter SDK version

[√] Flutter (Channel stable, 3.7.7, on Microsoft Windows [Version 10.0.22621.1555], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2022.1)
[√] VS Code (version 1.77.3)
[√] Connected device (4 available)
[√] HTTP Host Availability

Additional context AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.audio_demo"
    tools:ignore="Instantiatable">

    <permission
        android:name="android.permission.MEDIA_CONTENT_CONTROL"
        tools:ignore="ReservedSystemPermission" />

    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

    <application
        android:name="${applicationName}"
        android:icon="@mipmap/appicon_without_shadow"
        android:label="Demo"
        android:requestLegacyExternalStorage="true"
        android:theme="@style/AppTheme"
        tools:replace="android:theme">

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_notification_icon" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />

        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity
            android:name="com.ryanheise.audioservice.AudioServiceActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:exported="true"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:usesCleartextTraffic="true"
            android:windowSoftInputMode="adjustResize"
            tools:ignore="Instantiatable">

            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />

            <meta-data
                android:name="io.flutter.embedding.android.SplashScreenDrawable"
                android:resource="@drawable/launch_background" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

        <activity android:name="io.flutter.embedding.android.FlutterActivity" />

        <activity
            android:name="com.yalantis.ucrop.UCropActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.stripe.android.payments.StripeBrowserLauncherActivity"
            android:exported="true" />

        <!-- ADD THIS "SERVICE" element -->
        <service
            android:name="com.ryanheise.audioservice.AudioService"
            android:exported="true"
            android:foregroundServiceType="mediaPlayback"
            tools:ignore="Instantiatable">
            <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"
            tools:ignore="Instantiatable">
            <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="flutterEmbedding"
            android:value="2" />

    </application>
</manifest>
ryanheise commented 1 year ago

Here's the relevant Java code:


    private void enterPlayingState() {
        ContextCompat.startForegroundService(this, new Intent(AudioService.this, AudioService.class));
        if (!mediaSession.isActive())
            mediaSession.setActive(true);

        acquireWakeLock();
        mediaSession.setSessionActivity(contentIntent);
        internalStartForeground();
    }

    private void internalStartForeground() {
        startForeground(NOTIFICATION_ID, buildNotification());
        notificationCreated = true;
    }

As you can see, startForeground is called unconditionally following every call to startForegroundService unless some OTHER exception is occurring between the first and the second call. Since you didn't report any other exception happening between those two calls, I suspect there is something inconsistent with your report. Maybe either you are not using "the example" or maybe the reproduction steps are not exactly accurate?

jaythakkarBAPS commented 3 weeks ago

same issue face

ryanheise commented 3 weeks ago

Just looking at the error message, it seems to be reporting an error in a different audio plugin (com.dooboolab.TauEngine.FlautoBackgroundAudioService). Please report the bug to that project.

github-actions[bot] commented 1 week ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with just_audio.