Swift:如果加载了49个以上的声音文件,SwiftySound初始化错误 [英] Swift: SwiftySound Initialization Error if more than 49 sound files loaded

查看:104
本文介绍了Swift:如果加载了49个以上的声音文件,SwiftySound初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Swift中使用收藏夹视图"创建一个简单的音板,每个按钮代表一个可以播放的声音.结构如下(我知道,这可能不是最聪明的方法,但是直到我添加更多声音之前它才起作用):我有一个SoundFiles.swift和SoundFiles类,我声明了

I am trying to create a simple Soundboard in Swift with a Collection View, each button representing a sound that can be played. The structure is as follows (I know, it is probably not the smartest way to do it, but it worked earlier until I added some more sounds): I have a SoundFiles.swift with the SoundFiles class, which I declared

static let shared = SoundFiles()

它包含两个数组

let soundfiles: [String] = ["example_bla"]
let soundnames: [String] = ["example bla"]

以及

var translation: [String: String] = [:]

var currentSoundfiles: [String] = []

其中翻译"字典应该在文件名和屏幕上显示的内容之间有所不同,即,它与数组"soundfiles"和"soundnames"匹配.应用过滤器(搜索功能)后,"currentSoundfiles"数组将处理显示的声音文件. 在我的标签栏控制器中,我有一个包含以下内容的视图控制器

where the "translation" dictionary is supposed to make a difference between the names of the files and what's being displayed on the screen, i.e. it matches the arrays "soundfiles" and "soundnames". The "currentSoundfiles" array takes care of the displayed sound files after a filter (search function) is applied. In my Tab Bar Controller, I have a View Controller containing

var soundPlayers: [Sound?] = []

override func viewDidLoad() {
        super.viewDidLoad()

        setUpView()

        setupSwiftySound()

        setupDismissKeyboard()

        SoundFiles.shared.currentSoundfiles = SoundFiles.shared.soundfiles
        SoundFiles.shared.findTranslation()

        fillDropDowns()
        fillSounds()
    }

override func viewDidAppear(_ animated: Bool) {
    refreshCollectionView()
}

其中

func fillSounds(){
    soundPlayers.removeAll()
    for (index, _) in SoundFiles.shared.currentSoundfiles.enumerated(){
        if let playingURL = Bundle.main.url(forResource: SoundFiles.shared.currentSoundfiles[index], withExtension: "wav"){
            soundPlayers.append(Sound(url: playingURL))
            soundPlayers[index]?.volume = SoundFiles.shared.volume
        }
    }
}

是唯一相关的功能.只要我包含49个或更少的声音文件,一切都可以正常工作.包括50个或更多的声音文件,多次发生以下错误/警告:

is the only relevant function. As long as I include 49 or fewer sound files, everything works perfectly fine. Including 50 or more sound files, the following error/warning occurs multiple times:

SwiftySound初始化错误:错误域= NSOSStatusErrorDomain 代码= -42(空)"

SwiftySound initialization error: Error Domain=NSOSStatusErrorDomain Code=-42 "(null)"

奇怪的是,我仍然可以运行前49个声音文件而不会出现问题(单击其他按钮根本没有任何作用),但是任何其他操作都会导致应用崩溃,例如尝试更改为该应用程序的其他View Controller或通过单击按钮旁边的"+"(通过下拉菜单实现)来检索声音文件的其他信息.尝试转到第二个视图控制器"选项卡时,崩溃错误如下:

What is weird is that I can still run the first 49 sound files without a problem (clicking on the other buttons just doesn't do anything), but any other action makes the app crash, e.g. trying to change to a different View Controller of the app or retrieving additional information of the sound file by clicking on a "+" next to the button (implemented with a DropDown Menu). The crash error when trying to go to the second View Controller Tab reads as follows:

由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:无法在捆绑包中加载NIB:"NSBundle(已加载)",名称为"fGY-5H-E9k-view-obO-1i-lrO",目录为"SbOne" .storyboardc'' ***首先抛出调用堆栈: (0x185e48ec4 0x185019a50 0x185d4f594 0x1b2c8fea8 0x1b2a208e0 0x1b2a2128c 0x1b2a21554 0x1b298dea8 0x1b298e1b0 0x1b298f140 0x1b2990440 0x1b2972630 0x1b349177c 0x18a444b7c 0x18a449b34 0x18a3a8598 0x18a3d6ec8 0x18a3d7d30 0x185dd87cc 0x185dd3460 0x185dd3a00 0x185dd31f0 0x18804c584 0x1b2fe8c00 0x102474838 0x185892bb4) libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'fGY-5H-E9k-view-obO-1i-lrO' and directory 'SbOne.storyboardc'' *** First throw call stack: (0x185e48ec4 0x185019a50 0x185d4f594 0x1b2c8fea8 0x1b2a208e0 0x1b2a2128c 0x1b2a21554 0x1b298dea8 0x1b298e1b0 0x1b298f140 0x1b2990440 0x1b2972630 0x1b349177c 0x18a444b7c 0x18a449b34 0x18a3a8598 0x18a3d6ec8 0x18a3d7d30 0x185dd87cc 0x185dd3460 0x185dd3a00 0x185dd31f0 0x18804c584 0x1b2fe8c00 0x102474838 0x185892bb4) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

和XCode将我带到显示

and XCode brings me to the AppDelegate.swift showing

线程1:信号SIGABRT

Thread 1: signal SIGABRT

问题与包括哪些声音文件无关,仅取决于数量.有人知道这里发生了什么吗?

The problem is independent of which sound files are included, it just depends on the number. Does anybody have an idea of whats happening here?

推荐答案

正如@DavidPasztor指出的那样,问题在于内存问题,因为我一次打开了太多声音文件.将playersPerSound属性减小为1确实可以解决该问题,但是当然会在某些时候再次发生.我现在为我解决问题的方式如下:

As @DavidPasztor pointed out, the problem was a memory issue because I opened too many sound files at once. Reducing the playersPerSound property to 1 indeed solved the problem but it would, of course, reoccur at some point. How I solved the issue for me now is as follows:

我将每个声音保留5个播放器,以使其能够在声音播放完成之前多次播放(对于我的其他Storyboard/View Controller来说,这很重要,用户应该从第一个Storyboard/View Controller中选择较短的声音列表)查看控制器以进行快速选择).在第一个Storyboard/View Controller中,我放弃了fillSounds函数,而是在应该播放声音时使用了以下代码:

I kept 5 players per sound to be able to play sounds more often than once before they are finished (important for my other Storyboards/View Controllers, where the user is supposed to choose a shorter list of sounds from the first Storyboard/View Controller for a quick selection). In the first Storyboard/View Controller, I got rid of the fillSounds function and instead used the following code when a sound is supposed to be played:

@objc func playSound(sender: UIButton){
    if soundPlayers.count >= 20 {
        soundPlayers.removeLast(10)
    }
    if let playingURL = Bundle.main.url(forResource: SoundFiles.shared.currentSoundfiles[sender.tag], withExtension: "wav"){
        soundPlayers.append(Sound(url: playingURL))
        soundPlayers[soundPlayers.count-1]?.volume = SoundFiles.shared.volume
        soundPlayers[soundPlayers.count-1]?.play()
    }
}

此代码段现在不再使用每种声音的5个播放器,而是如果再次播放相同声音的声音,则添加一个新播放器;如果大小达到20,则删除前10个播放器.控制器不受限制,即板上的声音数量不能超过20(或任何其他合理的数量).到目前为止,我还没有遇到这种方式的应用程序任何问题.

This code snippet now doesn't make use of the 5 players per sound but instead adds a new player for the same sound if it is played again and deletes the first 10 players if the size reaches 20. The other Storyboards/View Controllers stay untouched with the restriction that the number of sounds on the board cannot exceed 20 (or any other reasonable number). Up to now, I didn't encounter any problem with the App in that way.

这篇关于Swift:如果加载了49个以上的声音文件,SwiftySound初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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