/Classes/HardwareDemo.m
Objective C | 161 lines | 119 code | 32 blank | 10 comment | 5 complexity | cd49dd020d06ab2cea8a70573265d849 MD5 | raw file
Possible License(s): Apache-2.0
1// 2// HardwareDemo.m 3// ObjectAL 4// 5// Created by Karl Stenerud on 10-10-15. 6// 7 8#import "HardwareDemo.h" 9#import "ImageButton.h" 10#import "ObjectAL.h" 11#import "MainScene.h" 12#import "CCLayer+Scene.h" 13#import "CCLayer+AudioPanel.h" 14#import "LampButton.h" 15#import "VUMeter.h" 16 17 18#define kSpaceBetweenButtons 40 19#define kStartY 180 20 21@interface HardwareDemo (Private) 22 23/** Build the user interface. */ 24- (void) buildUI; 25 26/** Exit the demo. */ 27- (void) onExitPressed; 28 29/** Update method for the VU meters. */ 30- (void) vuStep; 31 32@end 33 34 35@implementation HardwareDemo 36 37- (id) init 38{ 39 if(nil != (self = [super initWithColor:ccc4(255, 255, 255, 255)])) 40 { 41 [self buildUI]; 42 } 43 return self; 44} 45 46- (void) dealloc 47{ 48 [route release]; 49 [super dealloc]; 50} 51 52- (void) buildUI 53{ 54 [self buildAudioPanelWithSeparator]; 55 [self addPanelTitle:@"Hardware Monitoring"]; 56 [self addPanelLine1:@"Use your volume buttons and silent switch."]; 57 [self addPanelLine2:@"Not supported in the simulator."]; 58 59 CGSize screenSize = [[CCDirector sharedDirector] winSize]; 60 CCLabel* label; 61 62 label = [CCLabel labelWithString:@"Route:" fontName:@"Helvetica-Bold" fontSize:18]; 63 label.anchorPoint = ccp(0, 0.5f); 64 label.position = ccp(44, 34); 65 [self addChild:label]; 66 67 routeLabel = [CCLabel labelWithString:@"-" fontName:@"Helvetica" fontSize:18]; 68 routeLabel.anchorPoint = ccp(0, 0.5f); 69 routeLabel.position = ccp(label.position.x + 60, label.position.y); 70 [self addChild:routeLabel]; 71 72 label = [CCLabel labelWithString:@"Volume:" fontName:@"Helvetica-Bold" fontSize:18]; 73 label.anchorPoint = ccp(0, 0.5f); 74 label.position = ccp(220, 34); 75 [self addChild:label]; 76 77 volumeLabel = [CCLabel labelWithString:@"0.0" fontName:@"Helvetica" fontSize:18]; 78 volumeLabel.anchorPoint = ccp(0, 0.5f); 79 volumeLabel.position = ccp(label.position.x + 74, label.position.y); 80 [self addChild:volumeLabel]; 81 82 muteLabel = [LampButton buttonWithText:@"Muted:" 83 font:@"Helvetica-Bold" 84 size:18 85 lampOnLeft:NO 86 target:nil 87 selector:nil]; 88 muteLabel.anchorPoint = ccp(0, 0.5f); 89 muteLabel.position = ccp(350, 34); 90 muteLabel.isTouchEnabled = NO; 91 [self addChild:muteLabel]; 92 93 leftMeter = [[[VUMeter alloc] init] autorelease]; 94 leftMeter.anchorPoint = ccp(0, 0); 95 leftMeter.position = ccp(100, 52); 96 [self addChild:leftMeter]; 97 98 rightMeter = [[[VUMeter alloc] init] autorelease]; 99 rightMeter.anchorPoint = ccp(0, 0); 100 rightMeter.position = ccp(250, 52); 101 [self addChild:rightMeter]; 102 103 // Exit button 104 ImageButton* button = [ImageButton buttonWithImageFile:@"Exit.png" target:self selector:@selector(onExitPressed)]; 105 button.anchorPoint = ccp(1,1); 106 button.position = ccp(screenSize.width, screenSize.height); 107 [self addChild:button z:250]; 108} 109 110- (void) onEnterTransitionDidFinish 111{ 112 [[OALSimpleAudio sharedInstance] playBg:@"ColdFunk.caf" loop:YES]; 113 [OALSimpleAudio sharedInstance].backgroundTrack.meteringEnabled = YES; 114 115 volume = [OALAudioSession sharedInstance].hardwareVolume; 116 muted = [OALAudioSession sharedInstance].hardwareMuted; 117 route = [[OALAudioSession sharedInstance].audioRoute retain]; 118 119 [volumeLabel setString:[NSString stringWithFormat:@"%.2f", volume]]; 120 muteLabel.isOn = [OALAudioSession sharedInstance].hardwareMuted; 121 [routeLabel setString:[NSString stringWithFormat:@"%@", route]]; 122 123 [self schedule:@selector(step) interval:0.1f]; 124 [self schedule:@selector(vuStep)]; 125} 126 127- (void) step 128{ 129 float newVolume = [OALAudioSession sharedInstance].hardwareVolume; 130 if(newVolume != volume) 131 { 132 volume = newVolume; 133 [volumeLabel setString:[NSString stringWithFormat:@"%.2f", volume]]; 134 } 135 136 muteLabel.isOn = [OALAudioSession sharedInstance].hardwareMuted; 137 138 NSString* newRoute = [OALAudioSession sharedInstance].audioRoute; 139 if(![newRoute isEqualToString:route]) 140 { 141 [route autorelease]; 142 route = [newRoute retain]; 143 [routeLabel setString:[NSString stringWithFormat:@"%@", route]]; 144 } 145} 146 147- (void) vuStep 148{ 149 OALAudioTrack* bg = [OALSimpleAudio sharedInstance].backgroundTrack; 150 [bg updateMeters]; 151 leftMeter.db = [bg averagePowerForChannel:0]; 152 rightMeter.db = [bg averagePowerForChannel:1]; 153} 154 155- (void) onExitPressed 156{ 157 self.isTouchEnabled = NO; 158 [[CCDirector sharedDirector] replaceScene:[MainLayer scene]]; 159} 160 161@end