Thursday, May 27, 2010

Adding Music to an iPhone App

The instructions for using the AVAudioPlayer in the iPhone Application Programming Guide are pretty solid. There are a few required steps, however, that are not made explicit:
  • A player instance variable needs to be added to your class, e.g. AVAudioPlayer *player.
  • The AVAudioPlayer class needs to be imported in the header file, i.e. add this line to the top of the class's header file: #import
  • Make your class a AVAudioPlayerDelegate. In your class's @interface declaration, add AVAudioPlayerDelegate to the list of implemented protocols.
  • Add the AVFoundation framework to your app's link libraries. In XCode's "Group & Files" pane, expand Targets > [your app name]. Right-click on Link Binary With Libraries and select Add > Existing Frameworks. Select AVFoundation.framework and click Add.
Also worth noting: here's a good tip on making the music fade out.

No comments: