Audio Tutorial for iOS: Playing Audio Programatically [2014 Edition]

Audrey Tam
Screenshot from BasicSounds sample project

Screenshot from BasicSounds sample project

This article is the third in a three-part Audio Tutorial series covering audio topics of interest to the iPhone developer.

So far in this Audio Tutorial series we’ve talked about the difference between file and data formats and how to convert and record audio on your Mac. Now we’ll get to the fun part – actually playing audio on your phone!

There are many ways to play audio on the iPhone – System Sound Services, AVAudioPlayer, Audio Queue Services, and OpenAL. Without outside support libraries, the two easiest ways by far are System Sound Services and AVAudioPlayer – so let’s talk about when you would (and wouldn’t) want to use those, and how you can use them.

System Sound Services

System Sound Services provides an extremely easy way to play short audio files. This is particularly useful for audio alerts and simple game sounds (such as making a ‘click’ when moving a game piece). All you have to do is the following:

NSString *pewPewPath = [[NSBundle mainBundle] 
    pathForResource:@"pew-pew-lei" ofType:@"caf"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &self.pewPewSound);
AudioServicesPlaySystemSound(self.pewPewSound);

It is important to define pewPewSound as an iVar or property, and not as a local variable so that you can dispose of it later in dealloc. It is declared as a SystemSoundID.

If you were to dispose of it immediately after AudioServicesPlaySystemSound(self.pewPewSound), then the sound would never play.

Doesn’t get much easier than that. However there are some strong drawbacks to this method:

  • It only supports audio data formats linear PCM or IMA4.
  • It only supports audio file formats CAF, AIF, or WAV.
  • The sounds must be 30 seconds or less in length
  • The sound plays at once.
  • Only one sound can play at a time
  • And more – see the Multimedia Programming Guide.

AVAudioPlayer

So what if you have an audio file encoded with AAC or MP3 that you want to play as background music? Another easy way to play music is via the AVAudioPlayer class. Since the AVAudioPlayer class is part of AVFoundation, you will need to @import AVFoundation into your project.
For the most part, it again is quite simple:

NSError *error;
self.backgroundMusicPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundMusicURL error:&error];
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];

There are many advantages of using AVAudioPlayer; it gives you a lot of bang for the buck. You can also play several sounds at once (using a different AVAudioPlayer for each sound), and you can play sounds even when your app is in the background.

However, the drawback of AVAudioPlayer is it can be extremely slow. If you tap a button and try to trigger a sound with AVAudioPlayer, there will be a noticeable small delay. But if that doesn’t matter to you (like for starting up background music), AVAudioPlayer is a fine choice. By the way, prepareToPlay is optional; if you don’t call it, it will be called implicitly when the AVAudioPlayer calls play.

And there are a couple other things to keep in mind:

  1. If you’re playing background music, you should check to see if other audio (like the iPod) is playing first, so you don’t have two layers of music going on at once!
  2. If a phone call arrives and the user chooses “Decline,” by default your AVAudioPlayer will stop. You can start it back up again by registering for the AVAudioPlayerDelegate and resuming music in the audioPlayerEndInterruption:withOptions method.

Sample Code

I put together some sample code showing how to use System Sound Services and AVAudioPlayer that you might want to check out. It also illustrates how to use AVAudioSessions, how to handle interruptions, and how to set audio to play in the background. And in addition to demonstrating those APIs, it has some mighty funky beats and a cool spaceship to boot. Pew-pew!

OpenAL

If you’re writing a game or another app where you want fine grained control of audio with low latency, you probably don’t want to use the above methods. Instead, you might want to use OpenAL, a cross-platform audio library supported by the iPhone.


OpenAL can be a beast with a steep learning curve. Luckily, Alex Restrepo has made a nice Sound Engine library using OpenAL that you can either use in your projects or use as a reference.

Another option is the Cocos2D game library, which includes an extremely easy to use sound engine that makes playing audio a snap. You can learn how to use it the tutorial on How To Make a Simple iPhone Game With Cocos2D 3.0.

Where to go from here?

That’s about all I’m going to cover about audio topics in iPhone programming for now – but keep in mind I’ve barely scratched the surface. If you’re interested in more, I’d recommend reading Apple’s docs, especially the Core Audio Overview and the Audio Session Programming Guide, and possibly digging into OpenAL a bit more. You might also like to look at the tutorial How To Make a Music Visualizer that contains some additional examples of iPhone audio.

I hope this Audio Tutorial series has been useful to other developers who may be new to audio concepts. Feel free to share any additional info you’re aware of regarding audio programming that may be useful to others!

Audrey Tam

Audrey Tam retired at the end of 2012 from a 25-year career as a computer science academic. Her teaching included Pascal, C/C++, Java, Java web services, web app development in php and mysql, user interface design and evaluation, and iOS programming. Before moving to Australia, she worked on Fortran and PL/1 simulation software at IBM's development lab in Silicon Valley. Audrey now teaches short courses in iOS app development to non-programmers, and organizes venues for Melbourne Cocoaheads monthly meetings.

User Comments

41 Comments

[ 1 , 2 , 3 ]
  • Great Tutorial!
    I was wondering if you will write a tutorial in the future about using OpenAL in swift for games? Both for playing background music and sound effects?
    joviol72
  • This can't handle interruptions! I followed this, and even tried the sample app on my iPhone 6 with 8.0 and again on 8.2. When a phone call comes in, it doesn't resume background audio.
    eebean2
  • Can you explain where to specifically add the background music code? I have three screens (main, game, 2nd game) that I would like to add different background music for each that loops. Which file would I add this to in an Xcode project? Thanks!
    Maverick96
  • Maverick96 wrote:Can you explain where to specifically add the background music code? I have three screens (main, game, 2nd game) that I would like to add different background music for each that loops. Which file would I add this to in an Xcode project? Thanks!


    you could add an argument to init, to set a backgroundMusicPath property that configureAudioSession uses; then each of your view controllers inits AudioController with a different path.
    Audrey
  • eebean2 wrote:This can't handle interruptions! I followed this, and even tried the sample app on my iPhone 6 with 8.0 and again on 8.2. When a phone call comes in, it doesn't resume background audio.


    the delegate methods for interruption were deprecated in ios 8; actually, this tutorial probably needs to be updated to use AVAudioSession and AVAudioSessionInterruptionNotification
    Audrey
  • joviol72 wrote:Great Tutorial!
    I was wondering if you will write a tutorial in the future about using OpenAL in swift for games? Both for playing background music and sound effects?


    thanks, glad you liked it! I have no plans to write that tutorial, and a quick glance through iOS Games By Tutorials indicates that uses AVAudioPlayer
    Audrey
  • mrad wrote:I am getting an "The operation couldnít be completed. (OSStatus error 1685348671.)" when i try to initialize the player, although NSLog says the file exists:
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:_audioFileName];
    NSLog(@"[fileManager fileExistsAtPath:filePath]: %hhd", [fileManager fileExistsAtPath:filePath]);
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:_outputFileURL error:&error2];
    if (error2) {
    NSLog(@"error initialising player - %@", [error2 localizedDescription]);
    }


    thanks for replying to mehulparmar4ever, and sorry I haven't been monitoring this forum!

    your problem might be related to this: http://stackoverflow.com/questions/2700 ... -url-fails -- use [NSURL URLWithString:] instead of [NSURL fileURLWithPath:]
    Audrey
  • Can you explain where to specifically add the background music code? I have three screens (main, game, 2nd game) that I would like to add different background music for each that loops. Which file would I add this to in an Xcode project? Thanks!
    you could add an argument to init, to set a backgroundMusicPath property that configureAudioSession uses; then each of your view controllers inits AudioController with a different path.

    Sorry to sound like a complete noob but could you show me an example?
    Would the code be placed in each .m/.h for that background music?

    What code goes into .m and what code goes into .h? On the .h file, where would the code be placed?

    Again, new to the whole coding thing and been working through buzztouch so still learning the ropes. :) I appreciate the help! Thanks!
    Maverick96
  • Maverick96 wrote:Can you explain where to specifically add the background music code? I have three screens (main, game, 2nd game) that I would like to add different background music for each that loops. Which file would I add this to in an Xcode project? Thanks!
    you could add an argument to init, to set a backgroundMusicPath property that configureAudioSession uses; then each of your view controllers inits AudioController with a different path.

    Sorry to sound like a complete noob but could you show me an example?
    Would the code be placed in each .m/.h for that background music?

    What code goes into .m and what code goes into .h? On the .h file, where would the code be placed?

    Again, new to the whole coding thing and been working through buzztouch so still learning the ropes. :) I appreciate the help! Thanks!


    bear with me, I haven't been coding in Obj C, since Swift was announced. I've initialled the main changes:

    In AudioController.h:
    - (instancetype)initWithURL: (NSURL *) backgroundMusicURL; // AT

    In AudioController.m:
    - (instancetype)initWithURL: (NSURL *) backgroundMusicURL // AT
    {
    self = [super init];
    if (self) {
    [self configureAudioSession];
    [self configureAudioPlayer: backgroundMusicURL]; // AT: pass URL arg
    [self configureSystemSound];
    }
    return self;
    }

    - (void)configureAudioPlayer: (NSURL *) backgroundMusicURL { // AT
    // Create audio player with background music
    // AT: move these two lines to ATBViewController.m viewDidLoad
    // NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"background-music-aac" ofType:@"caf"];
    // NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
    self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
    self.backgroundMusicPlayer.delegate = self; // We need this so we can restart after interruptions
    self.backgroundMusicPlayer.numberOfLoops = -1; // Negative number means loop forever
    }

    In ATBViewController.m:
    - (void)viewDidLoad {
    [super viewDidLoad];

    // AT: set backgroundMusicURL here
    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"background-music-aac" ofType:@"caf"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];

    // AT: use new initWithURL method
    self.audioController = [[AudioController alloc] initWithURL: backgroundMusicURL];
    [self.audioController tryPlayMusic];
    }
    Audrey
  • Hi Audrey,
    Can you help!
    I am on Xcode 6.2 and OS X 10.10.2 (14C1514)
    I can't for the life of me get the downloaded pew-pew-lei.caf to play - I am trying to figure out how to add simple sound alerts to my iOS App.
    I finally gave up debugging the app and am using the PlayGround below to troubleshoot.
    I still can't figure out why I get a "-1500" error from AudioServicesCreateSystemSoundID(...);
    -------------------------------------------------
    let fldr = "/Users/Sarma/Downloads";
    let sndNm = "pew-pew-lei";
    let extn = "caf";
    let s = (fldr+"/"+sndNm+"."+extn);

    var sndURL: CFURLRef =
    CFURLCreateWithFileSystemPath ( kCFAllocatorDefault, s, CFURLPathStyle.CFURLPOSIXPathStyle, Boolean(0) );
    println("initAudioFile(\(sndNm),\(extn)): sndURL = \(sndURL)");
    // ---------- "initAudioFile(pew-pew-lei,caf): sndURL = file:///Users/Sarma/Downloads/pew-pew-lei.caf"
    // ---------- Within "Terminal" this command Plays without a problem:- afplay "/Users/Sarma/Downloads/pew-pew-lei.caf"

    var soundID: SystemSoundID = SystemSoundID();
    let retval :OSStatus = AudioServicesCreateSystemSoundID( sndURL, &soundID );
    println("AudioServicesCreateSystemSoundID returned = " + retval.description );
    // ---------- retval is -1500 (not some string as in the API reference.
    println("playSound(): SystemSoundID = " + soundID.description);
    // ---------- soundID is zero for obvious reasons.
    AudioServicesPlayAlertSound(soundID);
    // ---------- No sound!
    Sarma
  • Sarma wrote:Hi Audrey,
    Can you help!
    I am on Xcode 6.2 and OS X 10.10.2 (14C1514)
    I can't for the life of me get the downloaded pew-pew-lei.caf to play - I am trying to figure out how to add simple sound alerts to my iOS App.
    I finally gave up debugging the app and am using the PlayGround below to troubleshoot.
    I still can't figure out why I get a "-1500" error from AudioServicesCreateSystemSoundID(...);
    -------------------------------------------------
    let fldr = "/Users/Sarma/Downloads";
    let sndNm = "pew-pew-lei";
    let extn = "caf";
    let s = (fldr+"/"+sndNm+"."+extn);

    var sndURL: CFURLRef =
    CFURLCreateWithFileSystemPath ( kCFAllocatorDefault, s, CFURLPathStyle.CFURLPOSIXPathStyle, Boolean(0) );
    println("initAudioFile(\(sndNm),\(extn)): sndURL = \(sndURL)");
    // ---------- "initAudioFile(pew-pew-lei,caf): sndURL = file:///Users/Sarma/Downloads/pew-pew-lei.caf"
    // ---------- Within "Terminal" this command Plays without a problem:- afplay "/Users/Sarma/Downloads/pew-pew-lei.caf"

    var soundID: SystemSoundID = SystemSoundID();
    let retval :OSStatus = AudioServicesCreateSystemSoundID( sndURL, &soundID );
    println("AudioServicesCreateSystemSoundID returned = " + retval.description );
    // ---------- retval is -1500 (not some string as in the API reference.
    println("playSound(): SystemSoundID = " + soundID.description);
    // ---------- soundID is zero for obvious reasons.
    AudioServicesPlayAlertSound(soundID);
    // ---------- No sound!


    First of all, thanks for updating this to Swift!

    Possible answer: Swift CFURLCreateWithFileSystemPath returns a CFURL! And AudioServicesCreateSystemSoundID expects the same

    Note: While looking for this answer, I found some stackoverflow posts about AudioServicesPlayAlertSound apparently not working in iOS8, and needing to turn up or unmute the volume.

    btw -1500 is one of the result codes, the very helpfully named kAudioServicesSystemSoundUnspecifiedError ;)
    Audrey
[ 1 , 2 , 3 ]

Other Items of Interest

Ray's Monthly Newsletter

Sign up to receive a monthly newsletter with my favorite dev links, and receive a free epic-length tutorial as a bonus!

Advertise with Us!

Our Books

Our Team

Tutorial Team

... 20 total!

Update Team

... 4 total!

Editorial Team

... 5 total!

Code Team

... 2 total!

Subject Matter Experts

... 4 total!