在iOS13中重新分配AVAudioPlayer实例会导致BAD_ACCESS运行时错误 [英] Re-Assigning instance of AVAudioPlayer in iOS13 leads to BAD_ACCESS runtime error

查看:159
本文介绍了在iOS13中重新分配AVAudioPlayer实例会导致BAD_ACCESS运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,很遗憾,我坚持了下来,并且在过去 3天内尝试了很多事情之后,我必须在这里寻求帮助.

Hey guys, unfortunately I stuck and after trying a lot of things the last three days, I have to ask for help here.

我有一个应用程序,已经在AppStore中使用了很多年,并且运行时没有崩溃(最新部署目标是iOS 12.4).我有一些代码可以在应用程序中的某些事件上播放声音.

I have an app, that is for many years in the AppStore and runs without any crashes (last deployment target iOS 12.4). I have some code for playing a sound on certain events in the app.

现在,我尝试升级适用于iOS 13的应用程序,并且未更改与该"playSound"相关的任何代码,当在真实设备上进行测试时,我总是会收到此运行时错误./em>.在 simulator 上不会发生.

Now I tried to upgrade my app for iOS 13 and without changing any code related to that "playSound" thing, I always get this runtime error, when testing on a real device. Does not happen on simulator.

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x48)

Thread 1: EXC_BAD_ACCESS (code=1, address=0x48)

:在将该问题标记为"<重复> "之前,请考虑这与iOS13的发布有关,因为在未发生之前而代码只是正常".

PLEASE: Before you mark that question as "duplicate", consider that this must have something to do with the release of iOS13, because before it didn’t happen and the code is just "usual".

这是我的代码,也在 gitHub 上.

Here is my code, also on gitHub.

我在ViewController中有一个属性,可以防止ARC取消分配我的AVAudioPlayer:

I have a property in my ViewController to prevent ARC deallocating my AVAudioPlayer:

private var mySoundPlayer: AVAudioPlayer = AVAudioPlayer()

我有一个例程,应该在其中执行播放声音"(当分配一个AVAudioPlayer的新实例时,会发生此错误.resourceURL不是问题,RE-ASSIGNING是这个问题,我在未分配新实例的情况下对其进行了测试,并且没有崩溃.

I have a routine, where the "play sound" should be performed (here happens the error, when assigning a new instance of AVAudioPlayer. The resourceURL is not the problem, the RE-ASSIGNING is the problem, I tested it with a new instance not being assigned and i did not crash.

// -------------------------------------------------
// MARK: Private Methods
// -------------------------------------------------

private func makeSoundEvent(_ soundEvent : SoundEvent) {

    guard Settings().getSound() == .soundON else { return }
    guard let urlToRessource : URL = soundEvent.getFileURLToSoundRessource() else { return }
    do {
        mySoundPlayer = try AVAudioPlayer(contentsOf: urlToRessource)
        try? AVAudioSession.sharedInstance().setActive(true)
        mySoundPlayer.play()
    }
    catch { print("Could not create AVPlayer for ressource \(urlToRessource)") }
}

在这里,我在viewDidLoad()中的某个地方调用了该子例程,延迟了2秒.

And here I have the call of that subroutine, somewhere in my viewDidLoad() with a delay of 2 seconds.

DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    self.makeSoundEvent(.startFanfare)
}

由于 async 线程,我认为它不起作用.但是由于它在主线程上,所以我认为这应该可行.

I think it somehow does not work because of the async thread. But since it is on the main thread, I thought this should work.

感谢您的任何建议!

推荐答案

只需删除初始化,它将起作用

Just remove the initialisation and it will work

私有var mySoundPlayer:AVAudioPlayer!

private var mySoundPlayer: AVAudioPlayer!

干杯!

这篇关于在iOS13中重新分配AVAudioPlayer实例会导致BAD_ACCESS运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆