AudioPlayer._setPlatformActive.setPlatform Null check operator used on a null value
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:
- 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.
this also happen to me
hi, any updates? @ryanheise @under3415
I need someone to submit a bug report with a minimal reproduction project and steps to reproduce.
this also happen to me,so how to resolve it ?
I don't know how to replicate this issue. It happens infrequently in the wild
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!
I have resolved these errors by using this approach:
- 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.
- Using
runZonedGuardedto 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.**
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.