2011-01-08 13:41:27 +08:00
|
|
|
/*
|
|
|
|
Copyright (c) 2010 Steve Oldmeadow
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-03-21 20:12:58 +08:00
|
|
|
#import "audio/ios/CDAudioManager.h"
|
2011-01-08 13:41:27 +08:00
|
|
|
|
2011-07-08 16:08:54 +08:00
|
|
|
NSString * const kCDN_AudioManagerInitialised = @"kCDN_AudioManagerInitialised";
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
//NSOperation object used to asynchronously initialise
|
|
|
|
@implementation CDAsynchInitialiser
|
|
|
|
|
|
|
|
-(void) main {
|
2012-04-19 14:35:52 +08:00
|
|
|
[super main];
|
|
|
|
[CDAudioManager sharedManager];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CDLongAudioSource
|
|
|
|
|
2012-06-26 11:02:19 +08:00
|
|
|
@synthesize audioSourcePlayer, audioSourceFilePath, delegate, backgroundMusic, paused;
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(id) init {
|
2012-04-19 14:35:52 +08:00
|
|
|
if ((self = [super init])) {
|
|
|
|
state = kLAS_Init;
|
|
|
|
volume = 1.0f;
|
|
|
|
mute = NO;
|
|
|
|
enabled_ = YES;
|
2012-06-26 11:02:19 +08:00
|
|
|
paused = NO;
|
2014-03-27 10:43:57 +08:00
|
|
|
stopped = NO;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return self;
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) dealloc {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDLongAudioSource - deallocating %@", self);
|
|
|
|
[audioSourcePlayer release];
|
|
|
|
[audioSourceFilePath release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) load:(NSString*) filePath {
|
2015-12-09 00:45:39 +08:00
|
|
|
//We have already loaded a file previously, check if we are being asked to load the same file
|
2012-04-19 14:35:52 +08:00
|
|
|
if (state == kLAS_Init || ![filePath isEqualToString:audioSourceFilePath]) {
|
|
|
|
CDLOGINFO(@"Denshion::CDLongAudioSource - Loading new audio source %@",filePath);
|
|
|
|
//New file
|
|
|
|
if (state != kLAS_Init) {
|
|
|
|
[audioSourceFilePath release];//Release old file path
|
|
|
|
[audioSourcePlayer release];//Release old AVAudioPlayer, they can't be reused
|
|
|
|
}
|
|
|
|
audioSourceFilePath = [filePath copy];
|
|
|
|
NSError *error = nil;
|
|
|
|
NSString *path = [CDUtilities fullPathFromRelativePath:audioSourceFilePath];
|
|
|
|
audioSourcePlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
|
|
|
|
if (error == nil) {
|
|
|
|
[audioSourcePlayer prepareToPlay];
|
|
|
|
audioSourcePlayer.delegate = self;
|
|
|
|
if (delegate && [delegate respondsToSelector:@selector(cdAudioSourceFileDidChange:)]) {
|
|
|
|
//Tell our delegate the file has changed
|
|
|
|
[delegate cdAudioSourceFileDidChange:self];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CDLOG(@"Denshion::CDLongAudioSource - Error initialising audio player: %@",error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Same file - just return it to a consistent state
|
|
|
|
[self pause];
|
|
|
|
[self rewind];
|
|
|
|
}
|
|
|
|
audioSourcePlayer.volume = volume;
|
|
|
|
audioSourcePlayer.numberOfLoops = numberOfLoops;
|
|
|
|
state = kLAS_Loaded;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) play {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (enabled_) {
|
2014-03-28 16:41:33 +08:00
|
|
|
systemPaused = NO;
|
|
|
|
paused = NO;
|
|
|
|
stopped = NO;
|
2012-04-19 14:35:52 +08:00
|
|
|
[audioSourcePlayer play];
|
|
|
|
} else {
|
|
|
|
CDLOGINFO(@"Denshion::CDLongAudioSource long audio source didn't play because it is disabled");
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) stop {
|
2014-03-28 16:41:33 +08:00
|
|
|
paused = NO;
|
|
|
|
stopped = YES;
|
2012-04-19 14:35:52 +08:00
|
|
|
[audioSourcePlayer stop];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) pause {
|
2014-03-28 16:41:33 +08:00
|
|
|
paused = YES;
|
2012-04-19 14:35:52 +08:00
|
|
|
[audioSourcePlayer pause];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) rewind {
|
2014-03-28 16:41:33 +08:00
|
|
|
paused = NO;
|
2012-04-19 14:35:52 +08:00
|
|
|
[audioSourcePlayer setCurrentTime:0];
|
2014-03-28 15:59:04 +08:00
|
|
|
[audioSourcePlayer play];
|
|
|
|
stopped = NO;
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) resume {
|
2014-03-27 10:43:57 +08:00
|
|
|
if (!stopped) {
|
2014-03-28 16:41:33 +08:00
|
|
|
paused = NO;
|
2014-03-27 10:43:57 +08:00
|
|
|
[audioSourcePlayer play];
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(BOOL) isPlaying {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (state != kLAS_Init) {
|
|
|
|
return [audioSourcePlayer isPlaying];
|
|
|
|
} else {
|
|
|
|
return NO;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) setVolume:(float) newVolume
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
volume = newVolume;
|
|
|
|
if (state != kLAS_Init && !mute) {
|
|
|
|
audioSourcePlayer.volume = newVolume;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(float) volume
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return volume;
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Audio Interrupt Protocol
|
|
|
|
-(BOOL) mute
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return mute;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) setMute:(BOOL) muteValue
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (mute != muteValue) {
|
|
|
|
if (mute) {
|
|
|
|
//Turn sound back on
|
|
|
|
audioSourcePlayer.volume = volume;
|
|
|
|
} else {
|
|
|
|
audioSourcePlayer.volume = 0.0f;
|
|
|
|
}
|
|
|
|
mute = muteValue;
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(BOOL) enabled
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return enabled_;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) setEnabled:(BOOL)enabledValue
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (enabledValue != enabled_) {
|
|
|
|
enabled_ = enabledValue;
|
|
|
|
if (!enabled_) {
|
|
|
|
//"Stop" the sounds
|
|
|
|
[self pause];
|
|
|
|
[self rewind];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(NSInteger) numberOfLoops {
|
2012-04-19 14:35:52 +08:00
|
|
|
return numberOfLoops;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) setNumberOfLoops:(NSInteger) loopCount
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
audioSourcePlayer.numberOfLoops = loopCount;
|
|
|
|
numberOfLoops = loopCount;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDLongAudioSource - audio player finished");
|
|
|
|
#if TARGET_IPHONE_SIMULATOR
|
|
|
|
CDLOGINFO(@"Denshion::CDLongAudioSource - workaround for OpenAL clobbered audio issue");
|
|
|
|
//This is a workaround for an issue in all simulators (tested to 3.1.2). Problem is
|
|
|
|
//that OpenAL audio playback is clobbered when an AVAudioPlayer stops. Workaround
|
|
|
|
//is to keep the player playing on an endless loop with 0 volume and then when
|
|
|
|
//it is played again reset the volume and set loop count appropriately.
|
|
|
|
//NB: this workaround is not foolproof but it is good enough for most situations.
|
|
|
|
player.numberOfLoops = -1;
|
|
|
|
player.volume = 0;
|
|
|
|
[player play];
|
|
|
|
#endif
|
|
|
|
if (delegate && [delegate respondsToSelector:@selector(cdAudioSourceDidFinishPlaying:)]) {
|
|
|
|
[delegate cdAudioSourceDidFinishPlaying:self];
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDLongAudioSource - audio player interrupted");
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDLongAudioSource - audio player resumed");
|
|
|
|
if (self.backgroundMusic) {
|
|
|
|
//Check if background music can play as rules may have changed during
|
|
|
|
//the interruption. This is to address a specific issue in 4.x when
|
|
|
|
//fast task switching
|
|
|
|
if([CDAudioManager sharedManager].willPlayBackgroundMusic) {
|
|
|
|
[player play];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
[player play];
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@interface CDAudioManager (PrivateMethods)
|
|
|
|
-(BOOL) audioSessionSetActive:(BOOL) active;
|
|
|
|
-(BOOL) audioSessionSetCategory:(NSString*) category;
|
|
|
|
-(void) badAlContextHandler;
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation CDAudioManager
|
|
|
|
#define BACKGROUND_MUSIC_CHANNEL kASC_Left
|
|
|
|
|
|
|
|
@synthesize soundEngine, willPlayBackgroundMusic;
|
|
|
|
static CDAudioManager *sharedManager;
|
|
|
|
static tAudioManagerState _sharedManagerState = kAMStateUninitialised;
|
|
|
|
static tAudioManagerMode configuredMode;
|
|
|
|
static BOOL configured = FALSE;
|
|
|
|
|
|
|
|
-(BOOL) audioSessionSetActive:(BOOL) active {
|
2012-04-19 14:35:52 +08:00
|
|
|
NSError *activationError = nil;
|
|
|
|
if ([[AVAudioSession sharedInstance] setActive:active error:&activationError]) {
|
|
|
|
_audioSessionActive = active;
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Audio session set active %i succeeded", active);
|
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
//Failed
|
|
|
|
CDLOG(@"Denshion::CDAudioManager - Audio session set active %i failed with error %@", active, activationError);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(BOOL) audioSessionSetCategory:(NSString*) category {
|
2012-04-19 14:35:52 +08:00
|
|
|
NSError *categoryError = nil;
|
|
|
|
if ([[AVAudioSession sharedInstance] setCategory:category error:&categoryError]) {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Audio session set category %@ succeeded", category);
|
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
//Failed
|
|
|
|
CDLOG(@"Denshion::CDAudioManager - Audio session set category %@ failed with error %@", category, categoryError);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
// Init
|
|
|
|
+ (CDAudioManager *) sharedManager
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
@synchronized(self) {
|
|
|
|
if (!sharedManager) {
|
|
|
|
if (!configured) {
|
|
|
|
//Set defaults here
|
|
|
|
configuredMode = kAMM_FxPlusMusicIfNoOtherAudio;
|
|
|
|
}
|
|
|
|
sharedManager = [[CDAudioManager alloc] init:configuredMode];
|
|
|
|
_sharedManagerState = kAMStateInitialised;//This is only really relevant when using asynchronous initialisation
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kCDN_AudioManagerInitialised object:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sharedManager;
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (tAudioManagerState) sharedManagerState {
|
2012-04-19 14:35:52 +08:00
|
|
|
return _sharedManagerState;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this to set up audio manager asynchronously. Initialisation is finished when sharedManagerState == kAMStateInitialised
|
|
|
|
*/
|
|
|
|
+ (void) initAsynchronously: (tAudioManagerMode) mode {
|
2012-04-19 14:35:52 +08:00
|
|
|
@synchronized(self) {
|
|
|
|
if (_sharedManagerState == kAMStateUninitialised) {
|
|
|
|
_sharedManagerState = kAMStateInitialising;
|
|
|
|
[CDAudioManager configure:mode];
|
|
|
|
CDAsynchInitialiser *initOp = [[[CDAsynchInitialiser alloc] init] autorelease];
|
|
|
|
NSOperationQueue *opQ = [[[NSOperationQueue alloc] init] autorelease];
|
|
|
|
[opQ addOperation:initOp];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
+ (id) alloc
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
@synchronized(self) {
|
|
|
|
NSAssert(sharedManager == nil, @"Attempted to allocate a second instance of a singleton.");
|
|
|
|
return [super alloc];
|
|
|
|
}
|
|
|
|
return nil;
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call this method before accessing the shared manager in order to configure the shared audio manager
|
|
|
|
*/
|
|
|
|
+ (void) configure: (tAudioManagerMode) mode {
|
2012-04-19 14:35:52 +08:00
|
|
|
configuredMode = mode;
|
|
|
|
configured = TRUE;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
2015-12-29 07:59:36 +08:00
|
|
|
-(BOOL) isOtherAudioPlaying
|
|
|
|
{
|
|
|
|
// AudioSessionGetProperty removed from tvOS 9.1
|
|
|
|
#if defined(CC_TARGET_OS_TVOS)
|
|
|
|
return false;
|
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
UInt32 isPlaying = 0;
|
|
|
|
UInt32 varSize = sizeof(isPlaying);
|
|
|
|
AudioSessionGetProperty (kAudioSessionProperty_OtherAudioIsPlaying, &varSize, &isPlaying);
|
|
|
|
return (isPlaying != 0);
|
2015-12-29 07:59:36 +08:00
|
|
|
#endif
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) setMode:(tAudioManagerMode) mode {
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
_mode = mode;
|
|
|
|
switch (_mode) {
|
|
|
|
|
|
|
|
case kAMM_FxOnly:
|
|
|
|
//Share audio with other app
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Audio will be shared");
|
|
|
|
//_audioSessionCategory = kAudioSessionCategory_AmbientSound;
|
|
|
|
_audioSessionCategory = AVAudioSessionCategoryAmbient;
|
|
|
|
willPlayBackgroundMusic = NO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAMM_FxPlusMusic:
|
|
|
|
//Use audio exclusively - if other audio is playing it will be stopped
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Audio will be exclusive");
|
|
|
|
//_audioSessionCategory = kAudioSessionCategory_SoloAmbientSound;
|
|
|
|
_audioSessionCategory = AVAudioSessionCategorySoloAmbient;
|
|
|
|
willPlayBackgroundMusic = YES;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAMM_MediaPlayback:
|
|
|
|
//Use audio exclusively, ignore mute switch and sleep
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Media playback mode, audio will be exclusive");
|
|
|
|
//_audioSessionCategory = kAudioSessionCategory_MediaPlayback;
|
|
|
|
_audioSessionCategory = AVAudioSessionCategoryPlayback;
|
|
|
|
willPlayBackgroundMusic = YES;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAMM_PlayAndRecord:
|
|
|
|
//Use audio exclusively, ignore mute switch and sleep, has inputs and outputs
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Play and record mode, audio will be exclusive");
|
|
|
|
//_audioSessionCategory = kAudioSessionCategory_PlayAndRecord;
|
|
|
|
_audioSessionCategory = AVAudioSessionCategoryPlayAndRecord;
|
|
|
|
willPlayBackgroundMusic = YES;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
//kAudioManagerFxPlusMusicIfNoOtherAudio
|
|
|
|
if ([self isOtherAudioPlaying]) {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Other audio is playing audio will be shared");
|
|
|
|
//_audioSessionCategory = kAudioSessionCategory_AmbientSound;
|
|
|
|
_audioSessionCategory = AVAudioSessionCategoryAmbient;
|
|
|
|
willPlayBackgroundMusic = NO;
|
|
|
|
} else {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Other audio is not playing audio will be exclusive");
|
|
|
|
//_audioSessionCategory = kAudioSessionCategory_SoloAmbientSound;
|
|
|
|
_audioSessionCategory = AVAudioSessionCategorySoloAmbient;
|
|
|
|
willPlayBackgroundMusic = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self audioSessionSetCategory:_audioSessionCategory];
|
|
|
|
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to work around various bugs introduced in 4.x OS versions. In some circumstances the
|
|
|
|
* audio session is interrupted but never resumed, this results in the loss of OpenAL audio when following
|
|
|
|
* standard practices. If we detect this situation then we will attempt to resume the audio session ourselves.
|
|
|
|
* Known triggers: lock the device then unlock it (iOS 4.2 gm), playback a song using MPMediaPlayer (iOS 4.0)
|
|
|
|
*/
|
|
|
|
- (void) badAlContextHandler {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (_interrupted && alcGetCurrentContext() == NULL) {
|
|
|
|
CDLOG(@"Denshion::CDAudioManager - bad OpenAL context detected, attempting to resume audio session");
|
|
|
|
[self audioSessionResumed];
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
- (id) init: (tAudioManagerMode) mode {
|
2012-04-19 14:35:52 +08:00
|
|
|
if ((self = [super init])) {
|
2015-12-29 07:59:36 +08:00
|
|
|
|
|
|
|
// 'delegate' not supported on tvOS
|
|
|
|
#if !defined(CC_TARGET_OS_TVOS)
|
|
|
|
//Initialise the audio session
|
2012-04-19 14:35:52 +08:00
|
|
|
AVAudioSession* session = [AVAudioSession sharedInstance];
|
|
|
|
session.delegate = self;
|
2015-12-29 07:59:36 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
_mode = mode;
|
|
|
|
backgroundMusicCompletionSelector = nil;
|
|
|
|
_isObservingAppEvents = FALSE;
|
|
|
|
_mute = NO;
|
|
|
|
_resigned = NO;
|
|
|
|
_interrupted = NO;
|
|
|
|
enabled_ = YES;
|
|
|
|
_audioSessionActive = NO;
|
|
|
|
[self setMode:mode];
|
|
|
|
soundEngine = [[CDSoundEngine alloc] init];
|
|
|
|
|
|
|
|
//Set up audioSource channels
|
|
|
|
audioSourceChannels = [[NSMutableArray alloc] init];
|
|
|
|
CDLongAudioSource *leftChannel = [[CDLongAudioSource alloc] init];
|
|
|
|
leftChannel.backgroundMusic = YES;
|
|
|
|
CDLongAudioSource *rightChannel = [[CDLongAudioSource alloc] init];
|
|
|
|
rightChannel.backgroundMusic = NO;
|
|
|
|
[audioSourceChannels insertObject:leftChannel atIndex:kASC_Left];
|
|
|
|
[audioSourceChannels insertObject:rightChannel atIndex:kASC_Right];
|
|
|
|
[leftChannel release];
|
|
|
|
[rightChannel release];
|
|
|
|
//Used to support legacy APIs
|
|
|
|
backgroundMusic = [self audioSourceForChannel:BACKGROUND_MUSIC_CHANNEL];
|
|
|
|
backgroundMusic.delegate = self;
|
|
|
|
|
|
|
|
//Add handler for bad al context messages, these are posted by the sound engine.
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(badAlContextHandler) name:kCDN_BadAlContext object:nil];
|
|
|
|
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) dealloc {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - deallocating");
|
|
|
|
[self stopBackgroundMusic];
|
|
|
|
[soundEngine release];
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
[self audioSessionSetActive:NO];
|
|
|
|
[audioSourceChannels release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
/** Retrieves the audio source for the specified channel */
|
|
|
|
-(CDLongAudioSource*) audioSourceForChannel:(tAudioSourceChannel) channel
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return (CDLongAudioSource*)[audioSourceChannels objectAtIndex:channel];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
/** Loads the data from the specified file path to the channel's audio source */
|
|
|
|
-(CDLongAudioSource*) audioSourceLoad:(NSString*) filePath channel:(tAudioSourceChannel) channel
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLongAudioSource *audioSource = [self audioSourceForChannel:channel];
|
|
|
|
if (audioSource) {
|
|
|
|
[audioSource load:filePath];
|
|
|
|
}
|
|
|
|
return audioSource;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(BOOL) isBackgroundMusicPlaying {
|
2012-04-19 14:35:52 +08:00
|
|
|
return [self.backgroundMusic isPlaying];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
//NB: originally I tried using a route change listener and intended to store the current route,
|
|
|
|
//however, on a 3gs running 3.1.2 no route change is generated when the user switches the
|
|
|
|
//ringer mute switch to off (i.e. enables sound) therefore polling is the only reliable way to
|
|
|
|
//determine ringer switch state
|
|
|
|
-(BOOL) isDeviceMuted {
|
|
|
|
|
2015-12-30 04:30:00 +08:00
|
|
|
#if TARGET_IPHONE_SIMULATOR || defined(CC_TARGET_OS_TVOS)
|
2012-04-19 14:35:52 +08:00
|
|
|
//Calling audio route stuff on the simulator causes problems
|
|
|
|
return NO;
|
|
|
|
#else
|
|
|
|
CFStringRef newAudioRoute;
|
|
|
|
UInt32 propertySize = sizeof (CFStringRef);
|
|
|
|
|
|
|
|
AudioSessionGetProperty (
|
|
|
|
kAudioSessionProperty_AudioRoute,
|
|
|
|
&propertySize,
|
|
|
|
&newAudioRoute
|
|
|
|
);
|
|
|
|
|
|
|
|
if (newAudioRoute == NULL) {
|
|
|
|
//Don't expect this to happen but playing safe otherwise a null in the CFStringCompare will cause a crash
|
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
CFComparisonResult newDeviceIsMuted = CFStringCompare (
|
|
|
|
newAudioRoute,
|
|
|
|
(CFStringRef) @"",
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
return (newDeviceIsMuted == kCFCompareEqualTo);
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
#pragma mark Audio Interrupt Protocol
|
|
|
|
|
|
|
|
-(BOOL) mute {
|
2012-04-19 14:35:52 +08:00
|
|
|
return _mute;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) setMute:(BOOL) muteValue {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (muteValue != _mute) {
|
|
|
|
_mute = muteValue;
|
|
|
|
[soundEngine setMute:muteValue];
|
|
|
|
for( CDLongAudioSource *audioSource in audioSourceChannels) {
|
|
|
|
audioSource.mute = muteValue;
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL) enabled {
|
2012-04-19 14:35:52 +08:00
|
|
|
return enabled_;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) setEnabled:(BOOL) enabledValue {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (enabledValue != enabled_) {
|
|
|
|
enabled_ = enabledValue;
|
|
|
|
[soundEngine setEnabled:enabled_];
|
|
|
|
for( CDLongAudioSource *audioSource in audioSourceChannels) {
|
|
|
|
audioSource.enabled = enabled_;
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(CDLongAudioSource*) backgroundMusic
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return backgroundMusic;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
//Load background music ready for playing
|
|
|
|
-(void) preloadBackgroundMusic:(NSString*) filePath
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
[self.backgroundMusic load:filePath];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) playBackgroundMusic:(NSString*) filePath loop:(BOOL) loop
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
[self.backgroundMusic load:filePath];
|
|
|
|
|
2012-06-21 11:42:49 +08:00
|
|
|
if (loop) {
|
|
|
|
[self.backgroundMusic setNumberOfLoops:-1];
|
|
|
|
} else {
|
|
|
|
[self.backgroundMusic setNumberOfLoops:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!willPlayBackgroundMusic || _mute) {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - play bgm aborted because audio is not exclusive or sound is muted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.backgroundMusic play];
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) stopBackgroundMusic
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
[self.backgroundMusic stop];
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) pauseBackgroundMusic
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
[self.backgroundMusic pause];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) resumeBackgroundMusic
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!willPlayBackgroundMusic || _mute) {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - resume bgm aborted because audio is not exclusive or sound is muted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:02:19 +08:00
|
|
|
if (![self.backgroundMusic paused]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[self.backgroundMusic resume];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) rewindBackgroundMusic
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
[self.backgroundMusic rewind];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) setBackgroundMusicCompletionListener:(id) listener selector:(SEL) selector {
|
2012-04-19 14:35:52 +08:00
|
|
|
backgroundMusicCompletionListener = listener;
|
|
|
|
backgroundMusicCompletionSelector = selector;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call this method to have the audio manager automatically handle application resign and
|
|
|
|
* become active. Pass a tAudioManagerResignBehavior to indicate the desired behavior
|
|
|
|
* for resigning and becoming active again.
|
|
|
|
*
|
|
|
|
* If autohandle is YES then the applicationWillResignActive and applicationDidBecomActive
|
|
|
|
* methods are automatically called, otherwise you must call them yourself at the appropriate time.
|
|
|
|
*
|
|
|
|
* Based on idea of Dominique Bongard
|
|
|
|
*/
|
|
|
|
-(void) setResignBehavior:(tAudioManagerResignBehavior) resignBehavior autoHandle:(BOOL) autoHandle {
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!_isObservingAppEvents && autoHandle) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:@"UIApplicationWillResignActiveNotification" object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:@"UIApplicationDidBecomeActiveNotification" object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:@"UIApplicationWillTerminateNotification" object:nil];
|
|
|
|
_isObservingAppEvents = TRUE;
|
|
|
|
}
|
|
|
|
_resignBehavior = resignBehavior;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
- (void) applicationWillResignActive {
|
2014-03-28 16:41:33 +08:00
|
|
|
_resigned = YES;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-12-09 00:45:39 +08:00
|
|
|
//Set the audio session to one that allows sharing so that other audio won't be clobbered on resume
|
2012-04-19 14:35:52 +08:00
|
|
|
[self audioSessionSetCategory:AVAudioSessionCategoryAmbient];
|
|
|
|
|
|
|
|
switch (_resignBehavior) {
|
|
|
|
|
|
|
|
case kAMRBStopPlay:
|
|
|
|
|
|
|
|
for( CDLongAudioSource *audioSource in audioSourceChannels) {
|
Fix bugs with music not resuming when iOS app is reactivated (#16178)
* Pause instead of stopping music on resign
The [audioSource stop] causes the music to be stopped, and therefore it fails to resume later on a call to [audioSource resume], due to the addition of the if (!stopped) check in resume added in https://github.com/cocos2d/cocos2d-x/commit/26a04b38f2bd879d5705b38f55470f9fd9423df6
* Don't re-pause music that has already been paused
In this situation:
1. Start game, music plays
2. Switch to Music app, game music paused, start other music
3. Switch to game, game music not resumed due to other music playing
4. Switch to Music app, stop other music
5. Switch back to game, game music should resume due to no other music playing
At step 5 the game music doesn't currently resume. This is because at step 4 when switching to the Music app, the game music gets re-paused (actually isPlaying is false, so systemPaused is set to NO, even though the music *is* still paused). This causes the music to not be resumed at step 5.
This change fixes this, by skipping the pause logic if systemPaused is already true.
Note that this is dependent on https://github.com/cocos2d/cocos2d-x/pull/16178
* Fix typo in previous fix.
2016-07-21 17:41:24 +08:00
|
|
|
if (!audioSource->systemPaused) {
|
|
|
|
if (audioSource.isPlaying) {
|
|
|
|
audioSource->systemPaused = YES;
|
|
|
|
audioSource->systemPauseLocation = audioSource.audioSourcePlayer.currentTime;
|
|
|
|
[audioSource pause];
|
|
|
|
} else {
|
|
|
|
//Music is either paused or stopped, if it is paused it will be restarted
|
|
|
|
//by OS so we will stop it.
|
|
|
|
audioSource->systemPaused = NO;
|
|
|
|
[audioSource stop];
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kAMRBStop:
|
|
|
|
//Stop music regardless of whether it is playing or not because if it was paused
|
|
|
|
//then the OS would resume it
|
|
|
|
for( CDLongAudioSource *audioSource in audioSourceChannels) {
|
|
|
|
[audioSource stop];
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - handled resign active");
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Called when application resigns active only if setResignBehavior has been called
|
|
|
|
- (void) applicationWillResignActive:(NSNotification *) notification
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
[self applicationWillResignActive];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
- (void) applicationDidBecomeActive {
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-03-28 16:41:33 +08:00
|
|
|
if (_resigned) {
|
2012-04-19 14:35:52 +08:00
|
|
|
_resigned = NO;
|
|
|
|
//Reset the mode incase something changed with audio while we were inactive
|
|
|
|
[self setMode:_mode];
|
|
|
|
switch (_resignBehavior) {
|
|
|
|
|
|
|
|
case kAMRBStopPlay:
|
|
|
|
|
|
|
|
//Music had been stopped but stop maintains current time
|
|
|
|
//so playing again will continue from where music was before resign active.
|
|
|
|
//We check if music can be played because while we were inactive the user might have
|
|
|
|
//done something that should force music to not play such as starting a track in the iPod
|
|
|
|
if (self.willPlayBackgroundMusic) {
|
|
|
|
for( CDLongAudioSource *audioSource in audioSourceChannels) {
|
|
|
|
if (audioSource->systemPaused) {
|
|
|
|
[audioSource resume];
|
|
|
|
audioSource->systemPaused = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - audio manager handled become active");
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Called when application becomes active only if setResignBehavior has been called
|
|
|
|
- (void) applicationDidBecomeActive:(NSNotification *) notification
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
[self applicationDidBecomeActive];
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Called when application terminates only if setResignBehavior has been called
|
|
|
|
- (void) applicationWillTerminate:(NSNotification *) notification
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - audio manager handling terminate");
|
|
|
|
[self stopBackgroundMusic];
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** The audio source completed playing */
|
|
|
|
- (void) cdAudioSourceDidFinishPlaying:(CDLongAudioSource *) audioSource {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - audio manager got told background music finished");
|
|
|
|
if (backgroundMusicCompletionSelector != nil) {
|
|
|
|
[backgroundMusicCompletionListener performSelector:backgroundMusicCompletionSelector];
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) beginInterruption {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - begin interruption");
|
|
|
|
[self audioSessionInterrupted];
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) endInterruption {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - end interruption");
|
|
|
|
[self audioSessionResumed];
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
2012-06-21 11:42:49 +08:00
|
|
|
#if __CC_PLATFORM_IOS >= 40000
|
2011-01-08 13:41:27 +08:00
|
|
|
-(void) endInterruptionWithFlags:(NSUInteger)flags {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - interruption ended with flags %i",flags);
|
|
|
|
if (flags == AVAudioSessionInterruptionFlags_ShouldResume) {
|
|
|
|
[self audioSessionResumed];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
-(void)audioSessionInterrupted
|
|
|
|
{
|
|
|
|
if (!_interrupted) {
|
2012-04-19 14:35:52 +08:00
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Audio session interrupted");
|
|
|
|
_interrupted = YES;
|
|
|
|
|
|
|
|
// Deactivate the current audio session
|
|
|
|
[self audioSessionSetActive:NO];
|
|
|
|
|
|
|
|
if (alcGetCurrentContext() != NULL) {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Setting OpenAL context to NULL");
|
|
|
|
|
|
|
|
ALenum error = AL_NO_ERROR;
|
|
|
|
|
|
|
|
// set the current context to NULL will 'shutdown' openAL
|
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
|
|
|
|
if((error = alGetError()) != AL_NO_ERROR) {
|
|
|
|
CDLOG(@"Denshion::CDAudioManager - Error making context current %x\n", error);
|
|
|
|
}
|
|
|
|
#pragma unused(error)
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)audioSessionResumed
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (_interrupted) {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Audio session resumed");
|
|
|
|
_interrupted = NO;
|
|
|
|
|
|
|
|
BOOL activationResult = NO;
|
|
|
|
// Reactivate the current audio session
|
|
|
|
activationResult = [self audioSessionSetActive:YES];
|
|
|
|
|
|
|
|
//This code is to handle a problem with iOS 4.0 and 4.01 where reactivating the session can fail if
|
|
|
|
//task switching is performed too rapidly. A test case that reliably reproduces the issue is to call the
|
|
|
|
//iPhone and then hang up after two rings (timing may vary ;))
|
|
|
|
//Basically we keep waiting and trying to let the OS catch up with itself but the number of tries is
|
|
|
|
//limited.
|
|
|
|
if (!activationResult) {
|
|
|
|
CDLOG(@"Denshion::CDAudioManager - Failure reactivating audio session, will try wait-try cycle");
|
|
|
|
int activateCount = 0;
|
|
|
|
while (!activationResult && activateCount < 10) {
|
|
|
|
[NSThread sleepForTimeInterval:0.5];
|
|
|
|
activationResult = [self audioSessionSetActive:YES];
|
|
|
|
activateCount++;
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Reactivation attempt %i status = %i",activateCount,activationResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alcGetCurrentContext() == NULL) {
|
|
|
|
CDLOGINFO(@"Denshion::CDAudioManager - Restoring OpenAL context");
|
|
|
|
ALenum error = AL_NO_ERROR;
|
|
|
|
// Restore open al context
|
|
|
|
alcMakeContextCurrent([soundEngine openALContext]);
|
|
|
|
if((error = alGetError()) != AL_NO_ERROR) {
|
|
|
|
CDLOG(@"Denshion::CDAudioManager - Error making context current%x\n", error);
|
|
|
|
}
|
|
|
|
#pragma unused(error)
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
+(void) end {
|
2012-04-19 14:35:52 +08:00
|
|
|
[sharedManager release];
|
|
|
|
sharedManager = nil;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
@implementation CDLongAudioSourceFader
|
|
|
|
|
|
|
|
-(void) _setTargetProperty:(float) newVal {
|
2012-04-19 14:35:52 +08:00
|
|
|
((CDLongAudioSource*)target).volume = newVal;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(float) _getTargetProperty {
|
2012-04-19 14:35:52 +08:00
|
|
|
return ((CDLongAudioSource*)target).volume;
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) _stopTarget {
|
2012-04-19 14:35:52 +08:00
|
|
|
//Pause instead of stop as stop releases resources and causes problems in the simulator
|
|
|
|
[((CDLongAudioSource*)target) pause];
|
2011-01-08 13:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(Class) _allowableType {
|
2012-04-19 14:35:52 +08:00
|
|
|
return [CDLongAudioSource class];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
@end
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
@implementation CDBufferManager
|
|
|
|
|
|
|
|
-(id) initWithEngine:(CDSoundEngine *) theSoundEngine {
|
2012-04-19 14:35:52 +08:00
|
|
|
if ((self = [super init])) {
|
|
|
|
soundEngine = theSoundEngine;
|
|
|
|
loadedBuffers = [[NSMutableDictionary alloc] initWithCapacity:CD_BUFFERS_START];
|
|
|
|
freedBuffers = [[NSMutableArray alloc] init];
|
|
|
|
nextBufferId = 0;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) dealloc {
|
2012-04-19 14:35:52 +08:00
|
|
|
[loadedBuffers release];
|
|
|
|
[freedBuffers release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(int) bufferForFile:(NSString*) filePath create:(BOOL) create {
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NSNumber* soundId = (NSNumber*)[loadedBuffers objectForKey:filePath];
|
|
|
|
if(soundId == nil)
|
|
|
|
{
|
|
|
|
if (create) {
|
|
|
|
NSNumber* bufferId = nil;
|
|
|
|
//First try to get a buffer from the free buffers
|
|
|
|
if ([freedBuffers count] > 0) {
|
|
|
|
bufferId = [[[freedBuffers lastObject] retain] autorelease];
|
|
|
|
[freedBuffers removeLastObject];
|
|
|
|
CDLOGINFO(@"Denshion::CDBufferManager reusing buffer id %i",[bufferId intValue]);
|
|
|
|
} else {
|
|
|
|
bufferId = [[NSNumber alloc] initWithInt:nextBufferId];
|
|
|
|
[bufferId autorelease];
|
|
|
|
CDLOGINFO(@"Denshion::CDBufferManager generating new buffer id %i",[bufferId intValue]);
|
|
|
|
nextBufferId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([soundEngine loadBuffer:[bufferId intValue] filePath:filePath]) {
|
|
|
|
//File successfully loaded
|
|
|
|
CDLOGINFO(@"Denshion::CDBufferManager buffer loaded %@ %@",bufferId,filePath);
|
|
|
|
[loadedBuffers setObject:bufferId forKey:filePath];
|
|
|
|
return [bufferId intValue];
|
|
|
|
} else {
|
|
|
|
//File didn't load, put buffer id on free list
|
|
|
|
[freedBuffers addObject:bufferId];
|
|
|
|
return kCDNoBuffer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//No matching buffer was found
|
|
|
|
return kCDNoBuffer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return [soundId intValue];
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
|
|
|
|
-(void) releaseBufferForFile:(NSString *) filePath {
|
2012-04-19 14:35:52 +08:00
|
|
|
int bufferId = [self bufferForFile:filePath create:NO];
|
|
|
|
if (bufferId != kCDNoBuffer) {
|
|
|
|
[soundEngine unloadBuffer:bufferId];
|
|
|
|
[loadedBuffers removeObjectForKey:filePath];
|
|
|
|
NSNumber *freedBufferId = [[NSNumber alloc] initWithInt:bufferId];
|
|
|
|
[freedBufferId autorelease];
|
|
|
|
[freedBuffers addObject:freedBufferId];
|
|
|
|
}
|
|
|
|
}
|
2011-01-08 13:41:27 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|