Skip to content

Commit 7a2c5ff

Browse files
committed
Suspend and Resume OALAudioTrack on Android
1 parent 3bf1531 commit 7a2c5ff

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

external/ObjectAL/AudioTrack/OALAudioTrack.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ BRIDGE_CLASS("com.spritebuilder.OALAudioTrack")
6767
bool autoPreload;
6868
bool paused;
6969
bool muted;
70+
#if __CC_PLATFORM_ANDROID
71+
bool suspended;
72+
#endif
7073
float gain;
7174
float pan;
7275
NSInteger numberOfLoops;

external/ObjectAL/AudioTrack/OALAudioTrack.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ - (id) init
242242
numberOfLoops = 0;
243243
currentTime = 0.0;
244244

245+
#if __CC_PLATFORM_ANDROID
246+
// We have to manually pause tracks
247+
suspended = NO;
248+
#endif
245249
[[OALAudioTracks sharedInstance] notifyTrackInitializing:self];
246250
[[OALAudioTracks sharedInstance] addSuspendListener:self];
247251
}
@@ -593,7 +597,6 @@ - (void) setSuspended:(bool) value
593597
* TODO: Need to find a way to avoid this situation.
594598
*/
595599
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
596-
#warning FIXME
597600
OPTIONALLY_SYNCHRONIZED(self)
598601
{
599602
if(value)
@@ -676,6 +679,21 @@ - (void) setSuspended:(bool) value
676679
}
677680
*/
678681
}
682+
#elif __CC_PLATFORM_ANDROID
683+
OPTIONALLY_SYNCHRONIZED(self)
684+
{
685+
if (value) {
686+
if ([player isPlaying]) {
687+
[player pause];
688+
suspended = YES;
689+
}
690+
} else {
691+
if (suspended) {
692+
[player start];
693+
suspended = NO;
694+
}
695+
}
696+
}
679697
#endif
680698
}
681699

external/ObjectAL/AudioTrack/OALAudioTracks.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ - (id) init
7171

7272
tracks = [[ALWeakArray alloc] initWithCapacity:10];
7373

74-
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
7574
[[OALAudioSession sharedInstance] addSuspendListener:self];
76-
#endif
7775

7876
// Bug: Need to constantly poll deviceCurrentTime or else it resets to 0
7977
// on devices (doesn't happen in simulator).
@@ -89,9 +87,7 @@ - (id) init
8987
- (void) dealloc
9088
{
9189
OAL_LOG_DEBUG(@"%@: Dealloc", self);
92-
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
9390
[[OALAudioSession sharedInstance] removeSuspendListener:self];
94-
#endif
9591
[deviceTimePoller invalidate];
9692

9793
as_release(tracks);

external/ObjectAL/Session/OALAudioSession.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@
3030
#import "ccMacros.h"
3131

3232
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
33-
3433
#import <Foundation/Foundation.h>
3534
#import <AVFoundation/AVFoundation.h>
35+
#endif
36+
3637
#import "SynthesizeSingleton.h"
3738
#import "OALSuspendHandler.h"
3839

3940

4041
/**
4142
* Handles the audio session and interrupts.
4243
*/
44+
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
4345
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
4446
@interface OALAudioSession : NSObject <AVAudioSessionDelegate, OALSuspendManager>
4547
#else
4648
@interface OALAudioSession : NSObject <OALSuspendManager>
4749
#endif
50+
#elif __CC_PLATFORM_ANDROID
51+
@interface OALAudioSession : NSObject <OALSuspendManager>
52+
#endif
4853
{
4954
/** The current audio session category */
5055
NSString* audioSessionCategory;
@@ -141,12 +146,17 @@
141146
*/
142147
@property(nonatomic,readwrite,assign) bool handleInterruptions;
143148

149+
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
144150
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
145151
/** Delegate that will receive all audio session events (WEAK reference).
146152
*/
147153
@property(nonatomic,readwrite,assign) id<AVAudioSessionDelegate> audioSessionDelegate;
148154
#endif
149155

156+
#elif __CC_PLATFORM_ANDROID
157+
#warning Not implemented
158+
#endif
159+
150160
/** If true, the audio session is active */
151161
@property(nonatomic,readwrite,assign) bool audioSessionActive;
152162

@@ -203,4 +213,3 @@ SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(OALAudioSession);
203213

204214
@end
205215

206-
#endif // __CC_PLATFORM_IOS

external/ObjectAL/Session/OALAudioSession.m

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929

3030
#import "OALAudioSession.h"
3131

32-
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
33-
34-
#import <AudioToolbox/AudioToolbox.h>
3532
#import "ObjectALMacros.h"
3633
#import "ARCSafe_MemMgmt.h"
3734
#import "OALNotifications.h"
35+
36+
#if __CC_PLATFORM_IOS || __CC_PLATFORM_MAC
37+
38+
#import <AudioToolbox/AudioToolbox.h>
3839
#import "IOSVersion.h"
3940

4041

@@ -777,8 +778,11 @@ - (void)inputIsAvailableChanged:(BOOL)isInputAvailable
777778

778779
@end
779780

780-
#else
781+
#endif /* __IPHONE_OS_VERSION_MAX_ALLOWED */
781782

783+
#elif __CC_PLATFORM_ANDROID // ANDROID
784+
#pragma mark Android OALAudioSession
785+
#import "CCDirector.h"
782786
@implementation OALAudioSession
783787

784788
#pragma mark Object Management
@@ -799,6 +803,10 @@ - (id) init
799803
honorSilentSwitch = NO;
800804

801805
self.audioSessionActive = YES;
806+
[[CCDirector sharedDirector] addObserver:self
807+
forKeyPath:@"isPaused"
808+
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
809+
context:NULL];
802810
}
803811
return self;
804812
}
@@ -828,6 +836,19 @@ - (void) dealloc
828836

829837
#pragma mark Suspend Handler
830838

839+
- (void)observeValueForKeyPath:(NSString *)keyPath
840+
ofObject:(id)object
841+
change:(NSDictionary *)change
842+
context:(void *)context {
843+
if ([keyPath isEqual:@"isPaused"]) {
844+
if ([[CCDirector sharedDirector] isPaused]) {
845+
[self setManuallySuspended:YES];
846+
} else {
847+
[self setManuallySuspended:NO];
848+
}
849+
}
850+
}
851+
831852
- (void) addSuspendListener:(id<OALSuspendListener>) listener
832853
{
833854
[suspendHandler addSuspendListener:listener];
@@ -885,6 +906,4 @@ - (void) forceEndInterruption
885906

886907
@end
887908

888-
#endif
889-
890-
#endif // __CC_PLATFORM_IOS/MAC
909+
#endif /* __CC_PLATFORM_IOS/MAC/ANDROID */

0 commit comments

Comments
 (0)