/Classes/HardwareDemo.m

http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 161 lines · 119 code · 32 blank · 10 comment · 5 complexity · cd49dd020d06ab2cea8a70573265d849 MD5 · raw file

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