Reputation: 1418
let path = Bundle.main.path(forResource: "bang.wav", ofType:nil)!
let url = URL(fileURLWithPath: path)
let sound = try AVAudioPlayer(contentsOf: url)
I found this nice example code for playing a sound. Question not answered there: where does bang.wav need to be located in order to be found by Bundle.main.path during development and debugging? If MacOS and iOS answers are different, both are of interest.
Upvotes: 5
Views: 2471
Reputation: 3702
Bundle resources are located in /Contents/Resources
and can be included in subdirectories as well. The function .path(forResource:)
automatically finds it for you.
As long as your file is drag-dropped in your Xcode project, a Build Phase entry will be added that automatically copies your file to the bundle Resource folder.
Upvotes: 4