这里是一个操场,可以测试一些音频编程。我创建了一个包含运动场文件的文件夹,并插入了Bowie.mp3,它在代码中以其名称进行了引用。但是,我收到一条错误消息,说在捆绑包中找不到音频文件。 我猜您是在操场上处理支持文件的方式,而不是Xode IDE。
将mp3文件放在哪里或如何附加? (请在下面输入完整代码) THX
import UIKit
import AVFoundation
import UIKit
import PlaygroundSupport
// create instance of AudiofileURL and check for existence
guard let myAudioFileURL = Bundle.main.url(forResource: "Bowie", withExtension: "mp3") else {
fatalError("Audiofile not in Bundle")
} //end of guard let else
// create audioFile instance and check for existence
var myAudioFile: AVAudioFile?
do{
myAudioFile = try AVAudioFile(forReading: myAudioFileURL)
} catch {
fatalError("cannot create AVAudioFile \(error)")
} //end of do catch AudioFile check
// II Preparing Audio settings
// II.1 Creating more instances to let audioFile work on: a AVAudioNode instance is assigned to AVAudioEngine instance
let myEngine = AVAudioEngine()
let myPlayerNode = AVAudioPlayerNode()
// II.2. AudioEngine start // Why necessary?
myEngine.attach(myPlayerNode)
//II.3 assigning audioFile to the audioEngine/audioNode complex...(-> Feels like lot of effort for playing a mp3. Where are the pros and cons?
// Setting some variables for the initialisation
let myFramePosition: AVAudioFramePosition = 2000
let myFrameCount: AVAudioFrameCount = 5000
// II.4 scheduling playerNode instance. But Why?
myPlayerNode.scheduleFile(myAudioFile!, at: nil){
myPlayerNode.play()
}