AVAudioPlayer爆裂和爆裂

squ*_*o10 12 avaudioplayer ios swift

我有一个类似西蒙的记忆游戏,其中播放一系列音调并且用户尝试重复音调.问题是,我不时会听到一声可怕的噼啪声.它可能只是在几个笔记之后或多达二十个.声音都是长度为2秒的wav文件.我循环通过十个玩家,以便没有任何声音被剪裁.(我已经尝试过多达50但是没有任何帮助.)我也尝试过实施,audioPlayerDidFinishPlaying这样我就可以在声音完成后停止播放器,但这也无济于事.最后,我补充说prepareToPlay()- 遗憾的是,没有区别.

一个有趣或可能没有关系的有趣问题是,在大约15个音符的序列之后,音频停止一起工作.该应用程序继续正常,但没有任何声音.

这是代码的音频部分:

func playSound(_ soundID: Int) {
        var soundName = ""

        switch soundID {
        case 0:
            soundName = "beep1"
        case 1:
            soundName = "beep2"
        case 2:
            soundName = "beep3"
        case 3:
            soundName = "beep4"
        case 4:
            soundName = "swish3"
        default:
            soundName = "clank"
        }

        guard let url = Bundle.main.url(forResource: soundName, withExtension: "wav") else { return }

        do {
            buttonSound.insert(try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.wav.rawValue), at: playerIndex)
            buttonSound[playerIndex].prepareToPlay()
            buttonSound[playerIndex].delegate = self

            if playerIndex < 10 {
                buttonSound[playerIndex].play()
                playerIndex += 1
            } else {
                buttonSound[playerIndex].play()
                playerIndex = 0
            }

        } catch {
            // error
        }
    }

    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        if flag {
            player.stop()
        }
    }
Run Code Online (Sandbox Code Playgroud)

/********UPDATE*********/

我根据我跑过的某个过时的例子将AVAudioPlayer创建移动到了自己的类.它解决了15音调问题后完全切断的音频,然而,裂缝和爆裂仍然存在!我也试着没有运气重新录制声音.

以下是根据需要创建AVAudioPlayers的类:

import UIKit
import AVFoundation

private var players: [AVAudioPlayer] = []

class AVAudioPlayerPool: NSObject {

    class func playerWithURL(url: URL) -> AVAudioPlayer? {

        let availablePlayers = players.filter { (player) -> Bool in
            return player.isPlaying == false && player.url == url
        }

        if let playerToUse = availablePlayers.first {
            playerToUse.prepareToPlay()
            return playerToUse
        }

        do {
            let newPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.wav.rawValue)
            players.append(newPlayer)
            newPlayer.prepareToPlay()
            return newPlayer

        } catch {
            return nil
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

播放声音很简单:

func playSound(_ soundID: Int) {
    var soundName = ""

    switch soundID {
    case 0:
        soundName = "tt1"
    case 1:
        soundName = "tt2"
    case 2:
        soundName = "tt3"
    case 3:
        soundName = "tt4"
    case 4:
        soundName = "swish3"
    default:
        soundName = "clank"
    }

    guard let url = Bundle.main.url(forResource: soundName, withExtension: "wav") else { return }
    let player = AVAudioPlayerPool.playerWithURL(url: url)
    player?.play()

}
Run Code Online (Sandbox Code Playgroud)

如果有人有任何想法 - 即使这只是下一步你可能会尝试...

squ*_*o10 1

经过几个小时的实验,我会说问题是由我选择的声音引起的 - 一个大的、空心的、带有混响的方波合成器声音。我首先尝试降低可能有帮助的思考水平。接下来我尝试了各种不同的声音,但一切都发出噼啪声。我认为这与我分配球员的方式有更多关系。一旦我解决了播放器问题,我就不想再回去尝试不同的声音了。直到我做到了……而且它有效!