ryanheise / just_audio

Audio Player
1.05k stars 674 forks source link

Crash Error when Play Audio with Background Just Audio #1038

Open zh4dev opened 1 year ago

zh4dev commented 1 year ago

Which API doesn't behave as documented, and how does it misbehave? Hi, I have an issue when play an audio and implement with just audio background, it caused error because the index cannot be found or null value. Can you explain where the problem is? It's been a weeks, when i upgrade the version to newer, the error always showing.

Minimal reproduction project Link: https://github.com/ryanheise/just_audio/blob/minor/just_audio_background/lib/just_audio_background.dart Line 515 >>

if (_repeatMode == AudioServiceRepeatMode.one) return index!;
    if (effectiveIndices.isEmpty) return -1;
    if (index! >= effectiveIndicesInv.length) return -1;
    final invPos = effectiveIndicesInv[index!];
    var newInvPos = invPos + offset;
    if (newInvPos >= effectiveIndices.length || newInvPos < 0) {
      if (_repeatMode == AudioServiceRepeatMode.all) {
        newInvPos %= effectiveIndices.length;
      } else {
        return -1;
      }
    }
    final result = effectiveIndices[newInvPos];
    return result;

To Reproduce (i.e. user steps, not code)

  1. Launch the app (https://apps.apple.com/id/app/as-salah/id1607255192?l=id)
  2. Click Al-Quran Section and Choose Text and Audio
  3. Select one the surah
  4. Click Play Audio
  5. The app cannot launch and the error shows below

Error messages

E/flutter (23306): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
E/flutter (23306): #0      _PlayerAudioHandler.getRelativeIndex (package:just_audio_background/just_audio_background.dart:518:14)
E/flutter (23306): #1      _PlayerAudioHandler.previousIndex (package:just_audio_background/just_audio_background.dart:511:28)
E/flutter (23306): #2      _PlayerAudioHandler.hasPrevious (package:just_audio_background/just_audio_background.dart:513:27)
E/flutter (23306): #3      _PlayerAudioHandler._broadcastState (package:just_audio_background/just_audio_background.dart:681:11)
E/flutter (23306): #4      _PlayerAudioHandler._init.<anonymous closure> (package:just_audio_background/just_audio_background.dart:358:7)
E/flutter (23306): #5      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (23306): #6      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (23306): #7      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (23306): #8      _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
E/flutter (23306): #9      _MapStream._handleData (dart:async/stream_pipe.dart:218:10)
E/flutter (23306): #10     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
E/flutter (23306): #11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (23306): #12     CastStreamSubscription._onData (dart:_internal/async_cast.dart:85:11)
E/flutter (23306): #13     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (23306): #14     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (23306): #15     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
E/flutter (23306): #16     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
E/flutter (23306): #17     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
E/flutter (23306): #18     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter (23306): #19     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

Expected behavior Music should be can play and the crash is not appear, you can try to add if else inside of getRelativeIndex to be like this, i try to change your code and it's work, the error caused index is null, you force that null value to can be use on this logic, then caused error

Screenshot 2023-08-03 at 10 37 20

Screenshots

Desktop (please complete the following information):

Smartphone (please complete the following information): Ios

Flutter SDK version

[✓] Flutter (Channel stable, 3.10.6, on macOS 13.0.1 22A400 darwin-arm64, locale en-ID)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.78.2)
[✓] Connected device (2 available)
[✓] Network resources

Additional context Anyway i'm sorry, if i emailing you, thanks ryan for your help, appreciate it. Is there any solution for solve this? please let me know, thank you

ryanheise commented 1 year ago

Launch the app

Which app? You didn't actually provide it.

Minimal reproduction project

It seems you deleted the instructions for this section, which is where you need to provide the link to the fork.

zh4dev commented 1 year ago

Launch the app

Which app? You didn't actually provide it.

Minimal reproduction project

It seems you deleted the instructions for this section, which is where you need to provide the link to the fork.

Sorry, i've update the information above, can you check please?

ryanheise commented 1 year ago

Again, you didn't actually follow the instructions by providing a link to the fork. Please read the instructions (all instructions) carefully. You have already taken up quite a bit of my time with this, so please read all of the instructions before taking the next step.

zh4dev commented 1 year ago

Again, you didn't actually follow the instructions by providing a link to the fork. Please read the instructions (all instructions) carefully. You have already taken up quite a bit of my time with this, so please read all of the instructions before taking the next step.

Hi Ryan, anyway sorry for late response, i found how to solve this, i'm using if/else condition inside of getRelativeIndex (line 515 on just_audio_background.dart), i've update the information above.

Can you please update your code, to solve this issue, so I don't use your project that I changed

ryanheise commented 1 year ago

I can't investigate this because you didn't provide a link to the fork. I mentioned something in my previous comment about reading all of the instructions before taking the next step. It seems you didn't do that because there is still no link to the fork.

zh4dev commented 1 year ago

I can't investigate this because you didn't provide a link to the fork. I mentioned something in my previous comment about reading all of the instructions before taking the next step. It seems you didn't do that because there is still no link to the fork.

I've update the information, on Minimal reproduction project section, is that what you need?

ryanheise commented 1 year ago

No, because you still didn't provide a link to the fork.

kaciula commented 2 months ago

I'm getting crash reports about this although I cannot reproduce it locally. I have crash reports for Android 13 and 14 for Motorola and Oppo.

Null check operator used on a null value
       at _PlayerAudioHandler.getRelativeIndex(just_audio_background.dart:627)
       at _PlayerAudioHandler.previousIndex(just_audio_background.dart:622)
       at _PlayerAudioHandler.hasPrevious(just_audio_background.dart:624)
       at _PlayerAudioHandler._broadcastState(just_audio_background.dart:792)
       at _PlayerAudioHandler.play(just_audio_background.dart:666)
       at CompositeAudioHandler.play(audio_service.dart:2154)
       at _JustAudioPlayer.play(just_audio_background.dart:241)
       at AudioPlayer._sendPlayRequest(just_audio.dart:990)
       at AudioPlayer._setPlatformActive.setPlatform(just_audio.dart:1490)