菜鸟教程小白 发表于 2022-12-11 22:15:28

ios - Swift:如果加载超过 49 个声音文件,则 SwiftySound 初始化错误


                                            <p><p>我正在尝试在 Swift 中创建一个带有 Collection View 的简单 Soundboard,每个按钮代表一个可以播放的声音。结构如下(我知道,这可能不是最聪明的方法,但在我添加更多声音之前它工作得更早):我有一个带有 SoundFiles 类的 SoundFiles.swift,我声明了它</p>

<pre><code>static let shared = SoundFiles()
</code></pre>

<p>它包含两个数组</p>

<pre><code>let soundfiles: = [&#34;example_bla&#34;]
let soundnames: = [&#34;example bla&#34;]
</code></pre>

<p>还有</p>

<pre><code>var translation: = [:]

var currentSoundfiles: = []
</code></pre>

<p>“翻译”字典应该在文件名称和屏幕上显示的内容之间产生差异,即它匹配数组“声音文件”和“声音名称”。 “currentSoundfiles”数组在应用过滤器(搜索功能)后处理显示的声音文件。
在我的标签栏 Controller 中,我有一个 ViewController ,其中包含</p>

<pre><code>var soundPlayers: = []

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()
}
</code></pre>

<p>在哪里</p>

<pre><code>func fillSounds(){
    soundPlayers.removeAll()
    for (index, _) in SoundFiles.shared.currentSoundfiles.enumerated(){
      if let playingURL = Bundle.main.url(forResource: SoundFiles.shared.currentSoundfiles, withExtension: &#34;wav&#34;){
            soundPlayers.append(Sound(url: playingURL))
            soundPlayers?.volume = SoundFiles.shared.volume
      }
    }
}
</code></pre>

<p> 是唯一相关的功能。只要我包含 49 个或更少的声音文件,一切正常。包括 50 个或更多的声音文件,多次出现以下错误/警告:</p>

<blockquote>
<p>SwiftySound initialization error: Error Domain=NSOSStatusErrorDomain
Code=-42 &#34;(null)&#34;</p>
</blockquote>

<p>奇怪的是,我仍然可以毫无问题地运行前 49 个声音文件(单击其他按钮没有任何作用),但是任何其他操作都会导致应用程序崩溃,例如尝试更改应用程序的不同 ViewController 或通过单击按钮旁边的“+”检索声音文件的附加信息(通过下拉菜单实现)。尝试转到第二个 ViewController 选项卡时的崩溃错误如下:</p>

<blockquote>
<p>Terminating app due to uncaught exception &#39;NSInternalInconsistencyException&#39;, reason: &#39;Could not load NIB in bundle: &#39;NSBundle(loaded)&#39; with name &#39;fGY-5H-E9k-view-obO-1i-lrO&#39; and directory &#39;SbOne.storyboardc&#39;&#39;
*** 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) </p>
</blockquote>

<p>XCode 将我带到 AppDelegate.swift 显示</p>

<blockquote>
<p>Thread 1: signal SIGABRT</p>
</blockquote>

<p>问题与包含哪些声音文件无关,仅取决于数量。有人知道这里发生了什么吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>正如@DavidPasztor 指出的那样,问题是内存问题,因为我一次打开了太多声音文件。将 <code>playersPerSound</code> 属性减少到 1 确实解决了问题,但它当然会在某个时候再次发生。我现在为我解决问题的方法如下:</p>

<p>我为每个声音保留了 5 个玩家,以便能够在完成之前多次播放声音(对于我的其他 Storyboards/View Controllers 很重要,用户应该从第一个 Storyboard 中选择较短的声音列表/查看 Controller 进行快速选择)。在第一个 Storyboard/View Controller 中,我去掉了 <code>fillSounds</code> 函数,而是在应该播放声音时使用以下代码:</p>

<pre><code>@objc func playSound(sender: UIButton){
    if soundPlayers.count &gt;= 20 {
      soundPlayers.removeLast(10)
    }
    if let playingURL = Bundle.main.url(forResource: SoundFiles.shared.currentSoundfiles, withExtension: &#34;wav&#34;){
      soundPlayers.append(Sound(url: playingURL))
      soundPlayers?.volume = SoundFiles.shared.volume
      soundPlayers?.play()
    }
}
</code></pre>

<p>此代码片段现在不使用每个声音的 5 个播放器,而是为相同声音添加一个新播放器,如果再次播放,则删除前 10 个播放器,如果大小达到 20。其他 Storyboards/View Controller 保持不变,限制板上的声音数量不能超过 20(或任何其他合理的数量)。到目前为止,我在 App 上没有遇到任何问题。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift:如果加载超过 49 个声音文件,则 SwiftySound 初始化错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54796613/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54796613/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift:如果加载超过 49 个声音文件,则 SwiftySound 初始化错误