1
votes

I am fresh to programming and have chosen to learn swift as my first language. I have started playing with the AVFoundation framework. In this case I am working with AVAudioPlayer.

I currently have audio files being loaded through NSURL using the file name and type but I would like to be able to load audio files for each instance of AVAudioPlayer from a list or menu that displays the files currently in the app's resource folder.

This is how I am currently loading my audio files:

var audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:     NSBundle.mainBundle().pathForResource( "test_kick", ofType: "wav")!),    error: nil)

I have searched for hours with no luck and really don't know where to start with this. Any help would be greatly appreciated!

1
Simon, I don't think an Asset Catalog is supposed to be used for Audio Files... Why are you using this method specifically?Shai Mishali
Thank you for the reply Shai. Sorry I was not specific. I mean I would like to load from my supporting files rather than Asset Catalog. I have a number of .wav files in my supporting files folder within xcode and I would like to be able to select which one I load to AVAudioPlayer from within the app I am building.Simon Plooy

1 Answers

2
votes

Simon, if I understand your question right, you know how to load a file from the assets, but you want to show a list of the files and play it after a click on a specific file, is that right?

Therefore you will first have to list the folder contents in an UITableView, and react on the click from that TableView to load the selected file into your AVPlayer...

NSFileManager.defaultManager().contentsOfDirectoryAtPath("your path", error: nil)

The above code will give you an array of files inside the path you already have, to be represented in an UITableView

Do you know how to build up a UITableViewDataSource and Delegate?