nstudio / nativescript-audio

:microphone: NativeScript plugin to record and play audio :musical_note:
Other
149 stars 104 forks source link

NS 7: Random crashes on iOS device EXC_BAD_ACCESS #179

Closed aaayushsingh closed 2 years ago

aaayushsingh commented 3 years ago

The plugin works fine on iOS simulator but when running on an actual device, I'm getting random crashes and sometimes the audio finishes playing but the completeCallback never executes and app crashes in a few seconds.

The crashes happen after the audio finishes playing and never during the playback

edit: And there are no logs, even after setting debug to true

tomups commented 3 years ago

Getting this too, so far no luck finding out why... it seems in debug mode the app doesn't crash even on device, only in release.

aaayushsingh commented 3 years ago

The issue can be replicated multiple times. When running with Xcode, it gives EXC_BAD_ACCESS

I tried tinkering with the code and the issue occurs with the AVAudioSessionDelegate. You can also see this in the screenshots of the crash log below that the error occurs at finishedPlaying.

My initial thought was this was because the WeakRef was somehow getting garbage collected(this was a past known issue with NativeScript) and attempts to access were thus resulting in EXC_BAD_ACCESS but I tried to change that and the crashes were still happening. If we however comment out the line that adds delegate to AVAudioPlayer, The app doesn't crash after the audio finishes playing.

Still not 100% sure about the cause of the error

Screen Shot 2020-12-22 at 2 20 26 PM Screen Shot 2020-12-22 at 2 19 54 PM Screen Shot 2020-12-22 at 2 17 20 PM Screen Shot 2020-12-22 at 2 05 30 PM

aaayushsingh commented 3 years ago

@tomtastico I made a super hacky(and bad) fix that in no way should be taken to production unless extremely urgent. However, in my case it was urgent so I was left with no choice.

You can check out my fork and to add it to project use tns plugin add https://github.com/aaayushsingh/nativescript-audio

But avoid this unless necessary

tomups commented 3 years ago

In the end we switched to this fork of nativescript-sound https://github.com/forgm/nativescript-sound and it works fine with no crashes in iOS. We don't need audio recording so this is enough for us.

govi2010 commented 2 years ago

@aaayushsingh did you find good solution ?

farfromrefug commented 2 years ago

@aaayushsingh @govi2010 reading your comment on the cause of the issue i think it is because the delegate is not retained on the JS side. Changing

this._player.delegate = TNSPlayerDelegate.initWithOwner(this);

to

this.delegate = TNSPlayerDelegate.initWithOwner(this);
this._player.delegate = this.delegate;

could make it not crash however you must add the clearing of the delegate once the object is released

govi2010 commented 2 years ago

@farfromrefug Thank you for guidance. I am trying this right now. If it works then I will send PR.