just_audio icon indicating copy to clipboard operation
just_audio copied to clipboard

AudioPlayer._setPlatformActive.setPlatform Null check operator used on a null value

Open under3415 opened this issue 2 years ago • 5 comments

Which API doesn't behave as documented, and how does it misbehave? AudioPlayer._setPlatformActive.setPlatform throws "Null check operator used on a null value" error

Minimal reproduction project "The example"

I cannot reproduce this bug. This occurs "in the wild", I get this error in crashlytics, quite regularly. Last time on Galaxy A03 Core on Android 12, but no rules, different devices and versions

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

  1. Play a short sound. In vast majority of cases this works fine, but sometimes it errors. This is my code where the error is triggered:
  try {
            await _soundPlayer.setAsset('assets/sounds/change_sides.mp3');
            _soundPlayer.play();
  } catch (ex, st) {
            FirebaseCrashlytics.instance
                .recordError(ex, st, reason: 'soundPlayerSides');
 }

Error messages

Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Null check operator used on a null value. Error thrown soundPlayerSides.
       at AudioPlayer._setPlatformActive.setPlatform + 1436(just_audio.dart:1436)

Expected behavior Handle a null or prevent the null from occurring? I don't really understand what is going on here, does not seem to be related to my code.

Screenshots N/A

Desktop (please complete the following information): N/A

Smartphone (please complete the following information):

  • Device: Galaxy A03 Core, but also others
  • OS: Android 12, but also others

Flutter SDK version

just_audio: ^0.9.35
[√] Flutter (Channel stable, 3.13.9, on Microsoft Windows [Version 10.0.22631.2506], locale en-NZ)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Android Studio (version 2022.3)
[√] VS Code (version 1.84.2)
[√] Connected device (3 available)
[√] Network resources

Additional context Sorry I cannot replicate this. Feel free to close if this is not useful.

under3415 avatar Nov 11 '23 20:11 under3415

this also happen to me

rymesaint avatar Apr 25 '24 06:04 rymesaint

hi, any updates? @ryanheise @under3415

bthnkckalpamis avatar May 08 '24 18:05 bthnkckalpamis

I need someone to submit a bug report with a minimal reproduction project and steps to reproduce.

ryanheise avatar May 09 '24 01:05 ryanheise

this also happen to me,so how to resolve it ?

wangxp423 avatar May 23 '24 02:05 wangxp423

I don't know how to replicate this issue. It happens infrequently in the wild

under3415 avatar May 23 '24 02:05 under3415

I have the same issue: _TypeError: Null check operator used on a null value in just_audio.dart in AudioPlayer._setPlatformActive.setPlatform at line 1465 within just_audio

It is not reproducible: it happens in production "in the wild" but not during development.

I haven't been able to identify a specific pattern: it happens on any Android version, and any device (does not happen on iOS).

In the last month, it has affected 5% of my active users (37k) at least one time.

It's been happening at least as far back as May 2023.

@ryanheise If I can provide any extra information or do some tests to help you debug this issue, please let me know!

louisdeveseleer avatar Aug 27 '24 17:08 louisdeveseleer

I have resolved these errors by using this approach:

  1. Checking that player is initialized before playing. Note initialized is set to false on dispose to prevent errors when the app is shutting down and the sounds attempts to play from an async call.
  2. Using runZonedGuarded to cach async errors
 Future<void> _initializeAudioPlayer() async {
   //called from initState
    runZonedGuarded(() async {
      _soundPlayer = AudioPlayer();
     //other init settings...
       _initialized = true;
    }, (error, stackTrace) {
      FirebaseCrashlytics.instance.recordError(error, stackTrace, fatal: true);
    });
  }
 @override
  dispose() {
    initialized = false;
    try {
      _soundPlayer.dispose();
    } catch (ex, st) {
      FirebaseCrashlytics.instance
          .recordError(ex, st, reason: 'workoutDispose');
    }
    super.dispose();
  }

 void _loadAndPlaySound(String path, String reason) {
    if (!initialized) {
      return;
    }
    runZonedGuarded(() async {
      await _soundPlayer.setAsset(path);
      _soundPlayer.play(); //intentionally not awaiting here to play async
    }, (error, stackTrace) {
      // Handle the error
      FirebaseCrashlytics.instance
          .recordError(error, stackTrace, reason: reason);
    });
  }

Also, if obfuscating add this to R8 rules

# Just Audio
-keep class com.ryanheise.just_audio.** { *; }
-dontwarn com.ryanheise.just_audio.**

under3415 avatar Sep 19 '24 07:09 under3415

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.

github-actions[bot] avatar Oct 15 '24 00:10 github-actions[bot]