从其他堆栈溢出帖子中获得了我的大多数信息,但没有真正帮助。
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: "Music", withExtension: "m4a")
我想我看到了这个问题:您以“ .wav”而不是“ wav”的形式输入]
答案•删除字符串中的句点:“ .wav”