Audio_service: NullPointerException when stopping the audio on back button pressed

Created on 13 Jun 2020  Â·  16Comments  Â·  Source: ryanheise/audio_service

Describe the bug
I just started using this library and I'm following the provided sample. My app has a list of podcasts. Background playback is required; However, I want to stop the audio when the user pressed the device's back button.

Minimal reproduction project
I'm using the provided sample code. Just wrapped the MainScreen widget with WillPopScope and

        onWillPop: () async {
          AudioService.stop();
          return false;
        });

To Reproduce
Steps to reproduce the behavior:

  1. play an audio
  2. press the back button
  3. See error

Error messages

E/MethodChannel#ryanheise.com/audioService( 6102): Failed to handle method call
E/MethodChannel#ryanheise.com/audioService( 6102): java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.MediaControllerCompat$f android.support.v4.media.session.MediaControllerCompat.i()' on a null object reference
E/MethodChannel#ryanheise.com/audioService( 6102):  at com.ryanheise.audioservice.c$c.onMethodCall(:492)
E/MethodChannel#ryanheise.com/audioService( 6102):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(:226)
E/MethodChannel#ryanheise.com/audioService( 6102):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(:85)
E/MethodChannel#ryanheise.com/audioService( 6102):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(:631)
E/MethodChannel#ryanheise.com/audioService( 6102):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#ryanheise.com/audioService( 6102):  at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#ryanheise.com/audioService( 6102):  at android.os.Looper.loop(Looper.java:165)
E/MethodChannel#ryanheise.com/audioService( 6102):  at android.app.ActivityThread.main(ActivityThread.java:6810)
E/MethodChannel#ryanheise.com/audioService( 6102):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#ryanheise.com/audioService( 6102):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
E/MethodChannel#ryanheise.com/audioService( 6102):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
E/flutter ( 6102): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.support.v4.media.session.MediaControllerCompat$f android.support.v4.media.session.MediaControllerCompat.i()' on a null object reference, null)
E/flutter ( 6102): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter ( 6102): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
E/flutter ( 6102): <asynchronous suspension>
E/flutter ( 6102): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
E/flutter ( 6102): #3      AudioService.stop (package:audio_service/audio_service.dart:819:20)
E/flutter ( 6102): #4      PodcastPlayerPage.build.<anonymous closure> (package:motiveex/screens/audio_player_page.dart:146:24)
E/flutter ( 6102): #5      ModalRoute.willPop (package:flutter/src/widgets/routes.dart:1263:26)
E/flutter ( 6102): <asynchronous suspension>
E/flutter ( 6102): #6      NavigatorState.maybePop (package:flutter/src/widgets/navigator.dart:3535:67)
E/flutter ( 6102): #7      _WidgetsAppState.didPopRoute (package:flutter/src/widgets/app.dart:1042:28)
E/flutter ( 6102): #8      WidgetsBinding.handlePopRoute (package:flutter/src/widgets/binding.dart:616:26)
E/flutter ( 6102): #9      WidgetsBinding._handleNavigationInvocation (package:flutter/src/widgets/binding.dart:644:16)
E/flutter ( 6102): #10     MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:409:55)
E/flutter ( 6102): #11     MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:377:54)
E/flutter ( 6102): #12     _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:199:33)
E/flutter ( 6102): #13     _invoke3.<anonymous closure> (dart:ui/hooks.dart:290:15)
E/flutter ( 6102): #14     _rootRun (dart:async/zone.dart:1184:13)
E/flutter ( 6102): #15     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter ( 6102): #16     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
E/flutter ( 6102): #17     _invoke3 (dart:ui/hooks.dart:289:10)
E/flutter ( 6102): #18     _dispatchPlatformMessage (dart:ui/hooks.dart:164:5)

Expected behavior
I need to stop the audio when the device's back button is pressed.

Screenshots

Runtime Environment (please complete the following information if relevant):

  • Device: Redmi Note 5, Xiaomi
  • OS: Android API 28

Flutter SDK version

[√] Flutter (Channel stable, v1.17.1, on Microsoft Windows [Version 10.0.17763.615], locale en-US)
    • Flutter version 1.17.1 at D:\flutter
    • Framework revision f7a6a7906b (4 weeks ago), 2020-05-12 18:39:00 -0700
    • Engine revision 6bc433c6b6
    • Dart version 2.8.2

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    • Android SDK at C:\Users\MohammadJavadPC\AppData\Local\Android\Sdk
    • Platform android-30, build-tools 30.0.0
    • ANDROID_HOME = C:\Users\MohammadJavadPC\AppData\Local\Android\Sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 46.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (1 available)
    • Redmi Note 5 • 4472020f • android-arm64 • Android 9 (API 28)

• No issues found!

Additional context
Thanks in advance

3 testing bug

All 16 comments

Can you please provide a link to your minimal reproduction project? Thanks.

Are you sure this is a bug in AudioService? It looks like you're asking a question about how to use the standard Flutter APIs to execute code when the user backs out of your app, but I'm not the best person to ask about Flutter APIs. If you supply a minimal reproduction project, I don't think that will lead to finding a bug in audio_service.

I'm using this sample and as I described above I have wrapped the Widget in WillPopScope.

No. I'm not asking about Flutter API. I know how I should perform the task when the back button is pressed. I used the same technique elsewhere in the app and it works fine.

It seems when the back button is pressed, AudioService loses the connection with MediaSession or something. None of the AudioService methods works there.

If you change your code from:

        onWillPop: () async {
          AudioService.stop();
          return false;
        });

to:

        onWillPop: () async {
          print("Yes I see this");
          AudioService.stop();
          return false;
        });

Do you see the print output in the logs?

If you do, then AudioService.stop is being called without success and that is a bug. If you don't, then this is not a bug in the plugin, it's a matter of how to use Flutter APIs.

Yeah. I have already checked it. If I change it to:

        onWillPop: () async {
          print("Yes I see this");
          await AudioService.stop();
          print("what about this?");
          return false;
        });

I can see the first prompt but not the second one:

I/flutter (16017): Yes I see this
E/MethodChannel#ryanheise.com/audioService(16017): Failed to handle method call
E/MethodChannel#ryanheise.com/audioService(16017): java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.MediaControllerCompat$f android.support.v4.media.session.MediaControllerCompat.i()' on a null object reference
E/MethodChannel#ryanheise.com/audioService(16017):  at com.ryanheise.audioservice.c$c.onMethodCall(:492)
E/MethodChannel#ryanheise.com/audioService(16017):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(:226)
E/MethodChannel#ryanheise.com/audioService(16017):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(:85)
E/MethodChannel#ryanheise.com/audioService(16017):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(:631)
E/MethodChannel#ryanheise.com/audioService(16017):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#ryanheise.com/audioService(16017):  at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#ryanheise.com/audioService(16017):  at android.os.Looper.loop(Looper.java:165)
E/MethodChannel#ryanheise.com/audioService(16017):  at android.app.ActivityThread.main(ActivityThread.java:6810)
E/MethodChannel#ryanheise.com/audioService(16017):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#ryanheise.com/audioService(16017):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
E/MethodChannel#ryanheise.com/audioService(16017):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

In that case, can you provide a link to your minimal reproduction project? Thanks.

Can you provide a link to the full project please?

The project is not public. Let me create a sample project for you.
By the way, I think I'm clear on how you could reproduce the bug.

Sorry, I didn't mean a link to your personal project, I mean a link to the full project that you are submitting as your minimal reproduction project. This should be github repo that I can clone. The easiest way is to fork this project and modify the example, that way I can see a diff in the git logs of what you have changed, and also helps me to catch any other changes that bug submitters sometimes leave out of their bug report if they just give me coding instructions. It also saves me time because I don't have unlimited time to spend reproducing problems without actual projects.

Instructions from the bug report template:

Minimal reproduction project
If the example project exhibits the bug, please mention that here, otherwise fork this project and modify the example to reproduce the bug. Provide the link to your repository here.

https://github.com/zjamshidi/audio_service

As you see the close button on top left works fine, but the back button produces the exception

@zjamshidi Thanks for the project, that will be helpful. Apologies for the delay, I am a bit busy on something at the moment but this will be the first thing I look into when I get a chance.

I have the same issue with that Exception

Hi @zjamshidi , can you try the solution that I posted for #366 ? Although it is an unrelated issue, the solution to that one seems to also prevent the error you're getting.

Thanks for the update. I will check it now.

On Android, it seems to be working. I didn't test it on iOS.

This code change is unlikely to affect iOS, so I'll close this issue as resolved. But if you do notice an issue with iOS, do let me know.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

austinbyron picture austinbyron  Â·  4Comments

suragch picture suragch  Â·  4Comments

trighomautumnatg picture trighomautumnatg  Â·  7Comments

ryanheise picture ryanheise  Â·  6Comments

mohammadne picture mohammadne  Â·  7Comments