Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 2.38 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ## Header
  2. #import <Cocoa/Cocoa.h>
  3.  
  4. @interface RecorderClass : NSObject {
  5.     NSMutableArray *pressTimes;
  6.     NSDate *lastPress;
  7.     NSTimer *replayTimer;
  8.     NSInteger *currentSoundNumber;
  9.  
  10.     // In here, put all the buttons the user might press
  11.     IBOutlet NSButton *sampleButton1;
  12.     IBOutlet NSButton *sampleButton2;
  13.     IBOutlet NSButton *sampleButton3;
  14.     // etc...
  15. }
  16.  
  17. - (IBAction)record:(id)sender;
  18. - (IBAction)addSound:(id)sender;
  19. - (IBAction)stopRecording:(id)sender;
  20. - (IBAction)play:(id)sender;
  21. - (void)playSound:(NSTimer *)timer;
  22.  
  23. @end
  24.  
  25. ## Main
  26. #import "RecorderClass.h"
  27.  
  28. @implementation RecorderClass
  29. - (IBAction)record:(id)sender {
  30.     [sender setTitle:@"Stop Recording"];
  31.     pressTimes = [NSMutableArray array];
  32.     lastPress = [NSDate now];
  33. }
  34.  
  35. - (IBAction)addSound:(id)sender {
  36.     [pressTimes addObject:[NSDictionary dictionaryWithObjectsAndKeys:sender, @"ButtonPressed", [lastPress timeIntervalSinceNow], @"TimeBetweenPresses", nil]];
  37.     lastPress = [NSDate now];
  38. }
  39.  
  40. - (IBAction)stopRecording:(id)sender {
  41.     [sender setTitle:@"Start Recording"];
  42.     lastPress = nil;
  43. }
  44.  
  45. - (IBAction)play:(id)sender {
  46.     currentSoundNumber = 0;
  47.     replayTimer = [[NSTimer scheduledTimerWithTimeInterval:[[pressTimes objectAtIndex:0] objectForKey:@"TimeBetweenPresses"] target:self selector:@selector(playSound:) userInfo:[[pressTimes objectAtIndex:0] objectForKey:@"ButtonPressed"] retain];
  48. }
  49.  
  50. - (void)playSound:(NSTimer *)timer {
  51.     NSButton *buttonPressed = [timer object];
  52.     if([buttonPressed isEqualTo:sampleButton1]) {
  53.         NSSound *soundToBePlayed = [NSSound soundNamed:@"sample sound 1.mp3"];
  54.         [soundToBePlayed play]; // This is asynchronious, so we need to use sound:didFinishPlaying: to release the sound
  55.         [sound setDelegate:self];
  56.     } // etc for other buttons, you get the picture
  57. }
  58.  
  59. - (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying {
  60.     [sound release];
  61.     [replayTimer invalidate];
  62.     [replayTimer release];
  63.        
  64.     if(currentSoundNumber < [pressTimes count]) {
  65.         replayTimer = [[NSTimer scheduledTimerWithTimeInterval:[[pressTimes objectAtIndex:currentSoundNumber] objectForKey:@"TimeBetweenPresses"] target:self selector:@selector(playSound:) userInfo:[[pressTimes objectAtIndex:currentSoundNumber] objectForKey:@"ButtonPressed"] retain];
  66.     } else {
  67.         replayTimer = nil;
  68.     }
  69. }
  70. @end