Asounding: How-To
This document explains how to handle playback of sounds and background music using Asounding. It’s pretty short, but that’s because Asounding is pretty simple.
Sound Effects
Asounding separates sound playback in two parts: sounds and sources.
A sound is the actual data from a file you load into memory using the ASNSound class.
A source is a playback channel which can play sounds: you assign a sound to an instance of an ASNSource object, then ask the source to play the sound. You may create as many sources objects as the OpenAL implementation from iPhone OS allows, and each can play one sound simultaneously with other sources.
You’ll probably want each source to have a different sound, but sources can also share the same sound.
// Create a source
ASNSource *source = [[ASNSource alloc] init];
// Set the source's sound
NSURL *soundURL = [NSURL urlWithString:@"soundFile.wav"];
source.sound = [ASNSound soundWithContentOfURL:soundURL];
// Play the sound
[source play];
Background Music
Create an ASNMusic object and set it as the music to play on the shared ASNEngine instance. Then invoke the playMusic method to start playback.
// Create a music object.
NSURL *musicURL = [NSURL urlWithString:@"musicFile.aac"];
ASNMusic *music = [[ASNMusic alloc] initWithContentOfURL:musicURL error:nil];
// Set the music and tell the engine to play it.
[ASNEngine sharedEngine].music = music;
[[ASNEngine sharedEngine] playMusic];
Only one background music can play at a given time.
How to handle background iPod playback?
With Asounding, the default settings makes it so that your background music won’t play if the iPod is already playing, and iPod playback will not be interrupted. (Note that the iPod does not prevent sound effects from playing.)
But what if you do want to interrupt the iPod music? Then you set the iPodPlaybackDisabled property on the shared engine instance to YES
and it’ll instantly stop the iPod music.
[ASNEngine sharedEngine].iPodPlaybackDisabled = YES;
Asounding with Xcode
To use Asounding in an Xcode project, you simply add the Asounding directory to the project. You also need to add the following frameworks:
- AudioToolbox
- AVFoundation
- OpenAL
The header file to include is “Asounding.h”. You can use the demo project as a reference.