How to add MP3 to my iOS / Xcode project?

I am trying to program an application containing several songs. Looking through various tutorials, they just say β€œadd your file to the project” or some of them, so I tried to add my MP3 to different places (including xcassets), but it is never copied to the output of my project or is not played.

In other words, this always returns nil:

let path = NSBundle.mainBundle().pathForResource(audioFile, ofType: "mp3") 

It seems like dead simple stuff, and it drives me crazy that it will not just play the file. I tried this in the root directory, in the directory that I create with the name "resources" (as several sources say "add it to resources") and even in xcassets. However, it always returns to zero.

Please note that I am in Xcode 7 (beta) developing the latest iOS and using Swift 2. The file size is about 53 MB.

Any help is much appreciated!

+7
ios xcode nsbundle
source share
1 answer

It's really that simple. The only thing you might have missed is that the audioFile should really only contain the name, not the file extension. But beyond that, it's as simple as dragging and dropping a file into your project, and you can play it back.

In the next gif, I have a code that plays an audio file and does some manipulation to answer this question . But basically it just downloads the audio file the same way you try it.

  • First I run the program to show that it actually crashes without a file
  • Then I dragged the file into Xcode
  • Launching the application works again as expected

Screencast

+8
source share

All Articles