iPhone SDK: Cross fade Sound using Core Audio-AVAudioPlayer

Crossfade sound: dim the first playing sound track & in parallel start and increase the volume of the second playing sound track.

Restrictions from Apple:

- AVAudioPlayer Class Reference:"Play multiple sounds simultaneously, one sound per audio player, with precise synchronization"

Codecs "There are two groups of playback codecs in iOS. The first group, listed in Table 2-3, includes highly efficient formats that you can use without restriction. That is, you can play more than one instance of each of these formats at the same time."

The Problem: Had 4 audio files that needed to be played depending on the stage the user was in the App. During the transition from one audio files to another, the sound needs to crossfade.
The solution:
1. For simplicity created 4 AVAudioPlayer class members as such: (can be done with 2 but the logic gets more complicated)


#import "MyPlayer.h"

#import <AVFoundation/AVFoundation.h>

@interface ImageExampleViewController : UIViewController {

MyPlayer *audioPlayerForest;

MyPlayer *audioPlayerForestCalm;

MyPlayer *audioPlayerCamp;

MyPlayer *audioPlayerTown;


@property(nonatomic , retain)MyPlayer *audioPlayerForest;

@property(nonatomic , retain)MyPlayer *audioPlayerForestCalm;

@property(nonatomic , retain)MyPlayer *audioPlayerCamp;

@property(nonatomic , retain)MyPlayer *audioPlayerTown;



2. MyPlayer is a subclass of AVAudioPlayer with one additional property storing the state of the sound:

#import <Foundation/Foundation.h>

#import <AVFoundation/AVFoundation.h>

typedef enum {

starting = 0,

started = 1,

stopping = 2,

stopped = 3

} statePlayer;

@interface MyPlayer : AVAudioPlayer {

statePlayer state ;


@property(nonatomic) statePlayer state ;




#import "MyPlayer.h"

@implementation MyPlayer

@synthesize state;

- (id)init


self = [super init];

if (self) {

// Initialization code here.


return self;












GammaPoint Tweets