Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audioplayers fails on iOS after ~750 mp3s with AVPlayerItem.Status.failed #746

Open
mikeesouth opened this issue Jan 24, 2021 · 11 comments
Open
Labels
bug platform-ios Affects the ios platform

Comments

@mikeesouth
Copy link

I'm using audioplayers in a production app. It works great but I just discovered a critical bug. After playing about 750 mp3 files, audioplayers keep throwing an error (AVPlayerItem.Status.failed) and no more sounds are played. This happens only on iOS and only on physical devices.

Full Description
This is a really strange bug (at least to me) and I'm not sure if I'm doing something wrong in my code but I do not think so. The code is working perfectly for Android and also perfectly for iOS for quite some time but after about 750 played mp3s, it crashes. It may of course be something else but I can reproduce this by playing any mp3 ~750 times in a loop. This is no matter if I create my own AudioPlayer or use AudioCache. It does not matter if the files are long (~10s or short ~1s). It doesn't matter if I'm in MEDIA_PLAYER mode or LOW_LATENCY mode. If I just loop the same sound for 2000 times it will crash after about 750 played mp3s.
I've tested with audioplayers 0.16.1 and 0.17.2, both have the same result.

Code to Reproduce
I've not been able to test this minimal reproducible example yet since I can only test it on a physical iOS device and I do not have an iOS device here right now. I will try it tomorrow and post the results. But this code should reproduce the bug. The minimal reproducible example would be to clone the example from this lib and change this code:

Text('Play Local Asset In Low Latency \'audio.mp3\':'),
_Btn(
  txt: 'Play',
  onPressed: () =>
	  audioCache.play('audio.mp3', mode: PlayerMode.LOW_LATENCY),
),

to

Text('Play Local Asset In Low Latency \'audio.mp3\':'),
_Btn(
  txt: 'Play',
  onPressed: () {
	  for (int i = 1; i < 2000; i++) {
		print('lapCount = $i');
		audioCache.play('audio.mp3', mode: PlayerMode.LOW_LATENCY);
		await Future.delayed(Duration(milliseconds: 50));
		//await Future.delayed(Duration(seconds: 3));
	  }
  }
),

Please note that the delay maybe isn't necessary, I'm not sure. I've used 50ms delay in our app. Use 3 seconds or so to prevent overlap of the sound but I do not think it matters for reproducing the bug.

Log Errors
No errors really, except that the AudioPlayer sends an error with message "AVPlayerItem.Status.failed" through the onPlayerError stream of the AudioPlayer.

Platforms
This seems to be a bug specific for iOS, maybe MacOS as well but I have not tested that OS. I've tested iOS 12.x and 14.x, both have the same error. I've tested on iPad mini 2nd gen, iPad 6th gen, iPad 8th gen - all have the same error.
I've used Flutter stable 1.22.5 for all

  • OS: iOS
  • OS version: iOS 12.x & 14.x
  • Device: only physical iOS devices, only tested on iPads, not iPhones but I guess the same problem is on iPhone
  • flutter version: 1.22.5 (stable channel)
  • audioplayers version: 0.16.1 & 0.17.2
  • release or not release: this is reproduced in development, also reproduced with --profile flag. Also, my end users are reporting this frequently from the release builds so I'm 99.99% sure that it also affects release builds.
  • does the error occur and does it have any peculiarities: only on iOS, not Android. It works perfectly for ~750 mp3s, then it crashes every time.
@mikeesouth mikeesouth added the bug label Jan 24, 2021
@mikeesouth
Copy link
Author

I have reproduced this with the example in this repo: https://github.com/mikeesouth/audioplayers/tree/issue-746

Note that it's a branch called issue-746 that is branched from 0.16.1. To reproduce the error, start the example and press any red play-button (there is one right at the start page). It will play a sound and overlap with the same sound again after 50ms so the sound output is quite annoying and "choppy", like a robot that has some kind of speech problem :)

The red play button will play a small mp3 file (test.mp3) 2000 times. On iOS simulators, Android emulators and Android physical devices - this works just fine. On physical iOS devices, it stops playing sounds after about 700-750 times, that is after about 1 minute. When it stops playing sounds, the print output from the app is similar to this (note that lap 731 and 732 works just fine, 733 gets error AVPlayerItemStatus.failed and all the following calls get the same error):

flutter: lapCount = 731
flutter: Playing _file.path = /var/mobile/Containers/Data/Application/A40D1FFA-12CF-4AED-B181-0BC03A676AD6/Library/Caches/sound-731.mp3
flutter: Deleting _file.path = /var/mobile/Containers/Data/Application/A40D1FFA-12CF-4AED-B181-0BC03A676AD6/Library/Caches/sound-731.mp3
flutter: Fatal Error: Callback lookup failed!
flutter: lapCount = 732
flutter: Playing _file.path = /var/mobile/Containers/Data/Application/A40D1FFA-12CF-4AED-B181-0BC03A676AD6/Library/Caches/sound-732.mp3
flutter: Deleting _file.path = /var/mobile/Containers/Data/Application/A40D1FFA-12CF-4AED-B181-0BC03A676AD6/Library/Caches/sound-732.mp3
flutter: Fatal Error: Callback lookup failed!
flutter: lapCount = 733
flutter: Playing _file.path = /var/mobile/Containers/Data/Application/A40D1FFA-12CF-4AED-B181-0BC03A676AD6/Library/Caches/sound-733.mp3
flutter: onPlayerError: AVPlayerItemStatus.failed
flutter: await _file.exists() = true
flutter: lapCount = 734
flutter: Playing _file.path = /var/mobile/Containers/Data/Application/A40D1FFA-12CF-4AED-B181-0BC03A676AD6/Library/Caches/sound-734.mp3
flutter: onPlayerError: AVPlayerItemStatus.failed
flutter: await _file.exists() = true

Currently, on line 64/65 in one_time_audio_player.dart, I just have a delay of 50ms. By uncommenting line 65 (return _completer.future;) the sounds will play in sequence instead. The log output is a bit easier to follow but a full run (2000 sounds) takes about 10.5 minutes and on iOS the problem occurs after about 750 errors, about 4 minutes.

Do you have any idea what's going on here @luanpotter ? I'm happy to submit a PR but I'm not sure why this is happening or how I can try to fix it.

@mikeesouth
Copy link
Author

Pretty sure I'm experiencing the same memory leak as described here: #141
I actually solved this by creating my own pool of audio players before reading about the AudioPool and it seems to be working, still a bit buggy though so some kind of RELEASE logic is probably necessary on iOS.

@alexgrusu
Copy link

alexgrusu commented Feb 1, 2021

I'm experiencing a similar issue on Android that is a physical device (Huawei P9 lite).
After playing ~220 wav files, the audio can't be heared anymore even if I dispose and reinitialize the player.
When restarting the app, everything works fine.

Some output from my log:
I/MediaPlayer(22109): [HSM] stayAwake true uid: 10294, pid: 22109
I/MediaPlayer(22109): Pid:22109 MediaPlayer::start
E/MediaPlayer(22109): error (1, -19)
E/MediaPlayer(22109): Error (1,-19)
I/MediaPlayer(22109): [HSM] stayAwake false uid: 10294, pid: 22109
E/MediaPlayer(22109): pause called in state 0, mPlayer(0x7ccb239540)
E/MediaPlayer(22109): error (-38, 0)
E/MediaPlayer(22109): Attempt to perform seekTo in wrong state: mPlayer=0x7ccb239540, mCurrentState=0
E/MediaPlayer(22109): error (-38, 0)
I/MediaPlayer(22109): [HSM] stayAwake false uid: 10294, pid: 22109
E/MediaPlayer(22109): Error (-38,0)
I/MediaPlayer(22109): [HSM] stayAwake false uid: 10294, pid: 22109
E/MediaPlayer(22109): Error (-38,0)
I/MediaPlayer(22109): [HSM] stayAwake false uid: 10294, pid: 22109
I/MediaPlayer(22109): [HSM] stayAwake false uid: 10294, pid: 22109
I/MediaPlayer(22109): [HSM] stayAwake false uid: 10294, pid: 22109
I/MediaPlayer(22109): Pid:22109 MediaPlayer destructor

@alexgrusu
Copy link

alexgrusu commented Feb 1, 2021

I tried both
voicePlayerCache ??= AudioCache();

and

voicePlayerCache ??= AudioCache(fixedPlayer: voicePlayer);

PS: is not reproducible on android emulator

@yanivshaked
Copy link

Any update on this issue? I'm getting the same error AVPlayerItem.Status.failed on iOS only after a few calls to audioPlayer.play when streaming audio.

@keyur005
Copy link

I'm getting the same error AVPlayerItem.Status.failed.

@dougkeefe
Copy link

dougkeefe commented Sep 8, 2022

As am I. Same error and similar circumstances. I have 36 Audioplayers. Error happens eventually and inconsistently. Same error code

VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: AVPlayerItem.Status.failed
#0      MethodChannelAudioplayersPlatform._doHandlePlatformCall
package:audioplayers_platform_interface/method_channel_audioplayers_platform.dart:174
#1      MethodChannelAudioplayersPlatform.platformCallHandler
package:audioplayers_platform_interface/method_channel_audioplayers_platform.dart:147
#2      MethodChannel._handleAsMethodCall
package:flutter/…/services/platform_channel.dart:404
#3      MethodChannel.setMethodCallHandler.<anonymous closure>
package:flutter/…/services/platform_channel.dart:397
#4      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure>
package:flutter/…/services/binding.dart:380
#5      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure>
package:flutter/…/services/binding.dart:377
#6      _invoke2.<anonymous closure> (dart:ui/hooks.dart:190:15)
#7      _rootRun (dart:async/zone.dart:1426:13)
#8      _CustomZone.run (dart:async/zone.dart:1328:19)
#9      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
#10     _invoke2 (dart:ui/hooks.dart:189:10)
#11     _ChannelCallbackRecord.invoke (dart:ui/channel_buffers.dart:42:5)
#12     _Channel.push (dart:ui/channel_buffers.dart:132:31)
#13     ChannelBuffers.push (dart:ui/channel_buffers.dart:329:17)
#14     PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:589:22)

@Gustl22 Gustl22 added the platform-ios Affects the ios platform label Sep 27, 2022
@Gustl22
Copy link
Collaborator

Gustl22 commented Sep 27, 2022

Is this still a thing in audioplayers: ^1.1.0 ? We are happy to review any merge requests

@Gustl22 Gustl22 added the waiting for report Wait for the author to respond to the conversation label Sep 27, 2022
@dvishalakshan
Copy link

Is this still a thing in audioplayers: ^1.1.0 ? We are happy to review any merge requests

Yes, I'm still getting the error in audioplayers: ^1.1.1. Only iOS getting this error.

@DamonChen117
Copy link

DamonChen117 commented Nov 5, 2022

1.1.1 and iOS
I created a long audio and seek, pause, resume.
At the same time, create short audio from time to time, play and release.
After a while, the long audio not working, with error flutter: Unexpected platform error: AVPlayerItem.Status.failed

Then I cached the short audio players, the error gone.

@Gustl22 Gustl22 removed the waiting for report Wait for the author to respond to the conversation label Nov 23, 2022
@Gustl22
Copy link
Collaborator

Gustl22 commented Nov 23, 2022

@dvishalakshan @DamonChen117 you may can reproduce it by modifying our example: https://github.com/bluefireteam/audioplayers/tree/main/packages/audioplayers/example

You may want to debug and provide a fix, too. Thank you :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug platform-ios Affects the ios platform
Projects
None yet
Development

No branches or pull requests

8 participants