【发布时间】:2018-06-09 13:46:41
【问题描述】:
我正在通过构建 Soundboard 应用来学习编码。在我稍微改变了曲调后,我正在使用audioEngine 播放一些声音。在我的viewDidLoad 中,我声明了我的variables:
var audioFile1 = AVAudioFile()
var audioFile2 = AVAudioFile()
if let filePath = Bundle.main.path(forResource: "PeteNope", ofType:
"mp3") {
let filePathURL = URL(fileURLWithPath: filePath)
setPlayerFile(filePathURL)
}
if let filePath2 = Bundle.main.path(forResource: "Law_WOW", ofType:
"mp3") {
let filePath2URL = URL(fileURLWithPath: filePath2)
setPlayerFile2(filePath2URL)
}
func setPlayerFile(_ fileURL: URL) {
do {
let file1 = try AVAudioFile(forReading: fileURL)
self.audioFile1 = file1
} catch {
fatalError("Could not create AVAudioFile instance. error: \(error).")
}
}
func setPlayerFile2(_ fileURL: URL) {
do {
let file2 = try AVAudioFile(forReading: fileURL)
self.audioFile2 = file2
} catch {
fatalError("Could not create AVAudioFile instance. error: \(error).")
}
}
然后我连接nodes如下:
audioEngine.attach(pitchPlayer)
audioEngine.attach(timePitch)
audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile1.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile1.processingFormat)
audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile2.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile2.processingFormat)
所以,我的问题是,由于变量除了数字之外都是相同的,有没有办法以编程方式编写它,这样我就不必单独声明 nodes。
【问题讨论】:
-
我在这里没有看到任何
nodes。你指的是哪些变量?你知道Arrays 存在吗?
标签: swift xcode variables avaudioengine