Created
April 28, 2015 18:09
-
-
Save ilanoh/12f0b4d9b554f36511c0 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
#import "ViewController.h" | |
@interface ViewController () { | |
AVAudioRecorder *recorder; | |
AVAudioPlayer *player; | |
} | |
@end | |
@implementation ViewController | |
@synthesize playButton; | |
- (void)viewDidLoad | |
{ | |
UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)]; | |
[gesture1 setMinimumPressDuration:1]; | |
[self.view addGestureRecognizer:gesture1]; | |
[super viewDidLoad]; | |
// Disable Stop/Play button when application launches | |
[playButton setEnabled:NO]; | |
// Set the audio file | |
NSArray *pathComponents = [NSArray arrayWithObjects: | |
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], | |
@"MyAudioMemo.m4a", | |
nil]; | |
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents]; | |
// Setup audio session | |
AVAudioSession *session = [AVAudioSession sharedInstance]; | |
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; | |
// Define the recorder setting | |
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; | |
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; | |
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; | |
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; | |
// Initiate and prepare the recorder | |
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil]; | |
recorder.delegate = self; | |
recorder.meteringEnabled = YES; | |
[recorder prepareToRecord]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (void)longPressed:(UIGestureRecognizer *)longPress { | |
if(([longPress state] == UIGestureRecognizerStateEnded) || ([longPress state] == UIGestureRecognizerStateEnded)) { | |
NSLog(@"long press ended"); | |
[recorder stop]; | |
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; | |
[audioSession setActive:NO error:nil]; | |
} | |
else if([longPress state] == UIGestureRecognizerStateBegan) { | |
NSLog(@"long press detected"); | |
if (player.playing) { | |
[player stop]; | |
} | |
if (!recorder.recording) { | |
AVAudioSession *session = [AVAudioSession sharedInstance]; | |
[session setActive:YES error:nil]; | |
// Start recording | |
[recorder record]; | |
} | |
} | |
} | |
- (IBAction)playTapped:(id)sender { | |
if (!recorder.recording){ | |
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil]; | |
[player setDelegate:self]; | |
[player play]; | |
} | |
} | |
#pragma mark - AVAudioRecorderDelegate | |
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{ | |
[playButton setEnabled:YES]; | |
} | |
#pragma mark - AVAudioPlayerDelegate | |
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done" | |
message: @"Finish playing the recording!" | |
delegate: nil | |
cancelButtonTitle:@"OK" | |
otherButtonTitles:nil]; | |
[alert show]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment