Created
September 12, 2013 21:35
-
-
Save chrisallick/6544149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AnimalRecordView.m | |
// SoundFarm | |
// | |
// Created by chrisallick on 9/11/13. | |
// Copyright (c) 2013 GSP BETA Group. All rights reserved. | |
// | |
#import "AnimalRecordView.h" | |
@implementation AnimalRecordView | |
@synthesize recordButton, stopButton, arrowButton, arvViewDelegate; | |
- (id)initWithFrame:(CGRect)frame andSoundName:(NSString *)soundName andQuestion:(NSString *)questionName { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
hasRecorded = NO; | |
[self setBackgroundColor:[UIColor whiteColor]]; | |
name = [NSString stringWithFormat:@"%@", soundName]; | |
NSArray *dirPaths; | |
NSString *docsDir; | |
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
docsDir = [dirPaths objectAtIndex:0]; | |
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.caf", name]]; | |
soundFileURL = [NSURL fileURLWithPath:soundFilePath]; | |
NSDictionary *recordSettings = [NSDictionary | |
dictionaryWithObjectsAndKeys: | |
[NSNumber numberWithInt:AVAudioQualityMin], | |
AVEncoderAudioQualityKey, | |
[NSNumber numberWithInt:16], | |
AVEncoderBitRateKey, | |
[NSNumber numberWithInt: 2], | |
AVNumberOfChannelsKey, | |
[NSNumber numberWithFloat:44100.0], | |
AVSampleRateKey, | |
nil]; | |
NSError *error = nil; | |
audioRecorder = [[AVAudioRecorder alloc] | |
initWithURL:soundFileURL | |
settings:recordSettings | |
error:&error]; | |
if (error) { | |
NSLog(@"error: %@", [error localizedDescription]); | |
} else { | |
[audioRecorder prepareToRecord]; | |
} | |
NSURL *filePath = [[NSBundle mainBundle] URLForResource:soundName withExtension:@"gif"]; | |
UIImage *infoViewImage = [UIImage animatedImageWithAnimatedGIFURL:filePath]; | |
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/2-infoViewImage.size.width/4, 40.0, infoViewImage.size.width/2, infoViewImage.size.height/2)]; | |
[iv setImage:infoViewImage]; | |
[iv setUserInteractionEnabled:YES]; | |
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; | |
tapGesture.numberOfTapsRequired = 1; | |
[iv addGestureRecognizer:tapGesture]; | |
[self addSubview:iv]; | |
UIImage *copyImage = [UIImage imageNamed:questionName]; | |
UIImageView *copyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/-copyImage.size.width/4, 270, copyImage.size.width/2, copyImage.size.height/2)]; | |
[copyImageView setImage:copyImage]; | |
[self addSubview:copyImageView]; | |
stopButtonImage = [UIImage imageNamed:@"stop.png"]; | |
stopButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0, self.frame.size.height-stopButtonImage.size.height/2 - 20, stopButtonImage.size.width/2, stopButtonImage.size.height/2)]; | |
[stopButton setImage:stopButtonImage forState:UIControlStateNormal]; | |
[stopButton addTarget:self action:@selector(recordButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; | |
[stopButton setHidden:YES]; | |
[self addSubview:stopButton]; | |
recordButtonImage = [UIImage imageNamed:@"record.png"]; | |
recordButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0, self.frame.size.height-recordButtonImage.size.height/2 - 20, recordButtonImage.size.width/2, recordButtonImage.size.height/2)]; | |
[recordButton setImage:recordButtonImage forState:UIControlStateNormal]; | |
[recordButton addTarget:self action:@selector(recordButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; | |
[self addSubview:recordButton]; | |
arrowButtonImage = [UIImage imageNamed:@"arrow.png"]; | |
arrowButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-arrowButtonImage.size.width/2, self.frame.size.height-arrowButtonImage.size.height/2 - 20, arrowButtonImage.size.width/2, arrowButtonImage.size.height/2)]; | |
[arrowButton setImage:arrowButtonImage forState:UIControlStateNormal]; | |
[arrowButton addTarget:self action:@selector(arrowButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; | |
[arrowButton setHidden:YES]; | |
[self addSubview:arrowButton]; | |
} | |
return self; | |
} | |
- (void)handleSingleTap:(UISwipeGestureRecognizer *)recognizer { | |
[self playAudio]; | |
} | |
-(void) arrowButtonPressed:(id)sender { | |
[arvViewDelegate doneRecording]; | |
} | |
-(void) recordButtonPressed:(id)sender { | |
if( audioPlayer.isPlaying || audioRecorder.isRecording ) { | |
NSLog(@"stop"); | |
[self stop]; | |
} else if( hasRecorded ) { | |
NSLog(@"play"); | |
[self playAudio]; | |
} else { | |
NSLog(@"record"); | |
[self recordAudio]; | |
} | |
} | |
-(void) recordAudio { | |
if (!audioRecorder.recording) { | |
if( !hasRecorded ) { | |
hasRecorded = YES; | |
} | |
[recordButton setHidden:YES]; | |
[stopButton setHidden:NO]; | |
[audioRecorder record]; | |
} | |
} | |
-(void)stop { | |
if (audioRecorder.recording) { | |
[audioRecorder stop]; | |
} else if (audioPlayer.playing) { | |
[audioPlayer stop]; | |
} | |
[recordButton setHidden:YES]; | |
[arrowButton setHidden:NO]; | |
[stopButton setHidden:YES]; | |
} | |
-(void) playAudio { | |
if (!audioRecorder.recording) { | |
NSError *error; | |
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error]; | |
audioPlayer.delegate = self; | |
if (error) { | |
NSLog(@"Error: %@", [error localizedDescription]); | |
} else { | |
[audioPlayer play]; | |
} | |
} | |
} | |
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { | |
// [button setImage:playButtonImage forState:UIControlStateNormal]; | |
} | |
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { | |
NSLog(@"Decode Error occurred"); | |
} | |
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag { | |
//[button setImage:playButtonImage forState:UIControlStateNormal]; | |
} | |
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error { | |
NSLog(@"Encode Error occurred"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment