AsoundingReference

ASNEngine

Inherits fromNSObject
Declared inASNEngine.h
Part ofAsounding Audio Engine

Overview

Engine class handling music and and system events.

Discussion

The ASNEngine singleton is in charge of managing the audio state of the application. It stops and restarts the background music and sounds when the application is deactivated and then reactivated.

ASNEngine is also the application's background music player, allowing you to set the currently playing music, and do fade outs. Special care has been taken so that music playback is completely asynchrounous: starting playback or fading out won't cause the calling thread to block while.

Tasks

Getting the Engine

+ sharedEngine
Retrieve the shared engine instance.

Enabling Audio Playback

audioEnabled property
Master switch for disabling all audio playback (sound sources and music).
iPodPlaybackDisabled property
Whether iPod is allowed to play while this application is active.
musicEnabled property
Independent control to disable music alone.
soundsEnabled property
Independent control to disable sounds.

Checking Audio State

audioPlaybackSuspended property
Tell if Asounding audio playback is currently suspended.
audioSessionInterrupted property
Tell whether the audio session has been interrupted because of another audio session on the system.
iPodIsPlaying property
Tell whether the iPod is playing music or not.

Controlling Music Playback

- fadeMusicWithin:
Fade out and stop the current background music.
music property
The current background music.
musicVolume property
The playback volume for the background music.
- pauseMusic
Pause the current background music; keep current position.
playingMusic property
Tell if background music is currently playing.
- playMusic
Start playing the current background music.
- stopMusic
Stop the current background music; rewind to beginning.

Registering Sound Sources

- addSource:
Add a sound source to the engine.
- removeSource:
Remove a sound source from the engine. Should not be called directly.
sources property
The list of sound sources registered with the engine.

Properties

audioEnabled

Master switch for disabling all audio playback (sound sources and music).

@property (assign, getter=isAudioEnabled) BOOL audioEnabled

Discussion

Setting this to NO will suspend all audio output from Asounding: background music and currently running sounds effects are paused until audioEnabled is set again to YES.

Default value is YES.

audioPlaybackSuspended

Tell if Asounding audio playback is currently suspended.

@property (readonly) BOOL audioPlaybackSuspended

Discussion

Audio playback is suspended when the audioEnabled property is set to NO, when the application is deactivated (such as when the device is going to sleep) or when the audio session gets interrupted.

audioSessionInterrupted

Tell whether the audio session has been interrupted because of another audio session on the system.

@property (readonly, nonatomic, assign) BOOL audioSessionInterrupted

iPodIsPlaying

Tell whether the iPod is playing music or not.

@property (readonly, nonatomic, assign) UInt32 iPodIsPlaying

Discussion

This property is updated only when audio playback is suspended, which happens when you manually disable it or because of a system event (like the device going to sleep).

iPodPlaybackDisabled

Whether iPod is allowed to play while this application is active.

@property (assign, getter=isIPodPlaybackDisabled) BOOL iPodPlaybackDisabled

Discussion

Setting this to YES will force the iPod to stop playing music and allows Asounding to play its background music. Setting it to NO may block for a second while the iPod stops playing.

Default value is NO, which disables the background music when the iPod is playing.

music

The current background music.

@property (retain) ASNMusic * music

Discussion

This property contains the the music to be played in background. Changing its value will stop the current background music (if one was playing) and preload the new music.

musicEnabled

Independent control to disable music alone.

@property (getter=isMusicEnabled) BOOL musicEnabled

Discussion

Setting this to NO will disable music playback, pausing any currently playing music and make any call to playMusic wait until music is enabled again before starting playback.

Default value is YES.

musicVolume

The playback volume for the background music.

@property float musicVolume

Discussion

Changing this property will affect the volume at which the background music is played. Volume should be a float between 0.0 and 1.0. By default, this property is set to 0.5.

playingMusic

Tell if background music is currently playing.

@property (readonly) BOOL * playingMusic

Discussion

YES if background music from the Engine is currently playing, NO if not.

soundsEnabled

Independent control to disable sounds.

@property (assign, getter=isSoundsEnabled) BOOL soundsEnabled

Discussion

Setting this to NO will prevent stop currently playing sound sources and prevent sound sources from playing until set to YES again.

Default value is YES.

sources

The list of sound sources registered with the engine.

@property (readonly) NSSet * sources

Discussion

Sound sources when created automatically register themselves with the engine. This property lists all the sound sources currently in existance.

Class Methods

sharedEngine

Retrieve the shared engine instance.

+ (ASNEngine *)sharedEngine

Discussion

A shared instance of ASNEngine is created on the first call to this function.

Instance Methods

addSource:

Add a sound source to the engine.

- (void)addSource:(ASNSource *)newSource

Parameters

newSource
The sound source to add.

Discussion

This method should not be called directly. It is called by ASNSource during initialization.

fadeMusicWithin:

Fade out and stop the current background music.

- (void)fadeMusicWithin:(NSTimeInterval)seconds

Parameters

seconds
The number of seconds for the fade out effect.

Discussion

This method lowers the volume of the backgrond music progressively and then stops it, rewinding to the beginning. The time taken by the fade out effect is specified in seconds.

pauseMusic

Pause the current background music; keep current position.

- (void)pauseMusic

Discussion

This method stops the backgrond music, but keep the current position so that it can be restarted from here.

playMusic

Start playing the current background music.

- (void)playMusic

Discussion

This method starts the backgrond music immediately if there is no audio interruption and music is enabled, or else schedule the music to be started once music is enabled and audio interruptions are lifted.

Music will not start if the iPod is currently playing music, unless the shouldMusicPlayOverIPod property is set to YES. If the background music was previously paused, it restarts from the position it was paused, othewise it starts from the beginning.

removeSource:

Remove a sound source from the engine. Should not be called directly.

- (void)removeSource:(ASNSource *)theSource

Parameters

theSource
The sound source to remove.

Discussion

This method should not be called directly. It is called when by ASNSource before deallocation.

stopMusic

Stop the current background music; rewind to beginning.

- (void)stopMusic

Discussion

This method stops the backgrond music and rewind the current position to the beginning.