我的大部分信息都来自其他stackoverflow帖子,但没有一个真正有用。

  import UIKit
    import AVFoundation

    class FaceButtonScreen: UIViewController {

        @IBOutlet weak var mainButton1: UIButton!

        var audioPlayer: AVAudioPlayer = AVAudioPlayer()

不确定是否正确执行阵列
        var arrayOfSounds = ["sound1", "sound2", "sound3", "sound4", "sound5", "sound6", "sound7", "sound8"]

        func setupAudioPlayer(file: NSString, type: NSString){

            let path = Bundle.main.path(forResource: file as String, ofType: type as String)
            let url = NSURL.fileURL(withPath: path!)
            do {
                try audioPlayer = AVAudioPlayer(contentsOf: url)
            } catch {
                print("Player not available")
            }

        }


        }

这里有一个与按钮连接的不同功能,但是
显示的错误
    func faceButtonTapped(_ sender: UIButton) {
            let range: UInt32 = UInt32(arrayOfSounds.count)
            let number = Int(arc4random_uniform(range))
        let sound = arrayOfSounds.randomElement()
            self.setupAudioPlayer(arrayOfSounds[number], type: ".wav")
            self.audioPlayer?.play()



    //I need to understand how to complete the array and connect the sound files; any
     help would be greatly appreciated

最佳答案

这是我挖出的一些Swift代码:

let url = Bundle.main.url(forResource: "sound1", withExtension: "wav")

我想我看到了这个问题:
您以“.wav”而不是“wav”传递类型

回答
•删除字符串中的句点:“。wav”

关于arrays - 一个按钮的随机声音生成器,该按钮使用8个不同的.wav文件。每个文件都命名为sound1.wav,sound2.wav…sound8.wav,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61493206/

10-16 15:43