Last active
August 29, 2015 14:03
-
-
Save mikekavouras/f0293aadb5eb7b5fc6e5 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
// | |
// RCViewController.m | |
// recording | |
// | |
// Created by Mike Kavouras on 7/14/14. | |
// Copyright (c) 2014 Mike Kavouras. All rights reserved. | |
// | |
#import "RCViewController.h" | |
#import <AVFoundation/AVFoundation.h> | |
@interface RCViewController () <AVAudioPlayerDelegate, AVAudioRecorderDelegate> | |
@property (nonatomic, strong) AVAudioPlayer *audioPlayer; | |
@property (nonatomic, strong) AVAudioRecorder *audioRecorder; | |
@property (weak, nonatomic) IBOutlet UIButton *recordButton; | |
@property (weak, nonatomic) IBOutlet UIButton *stopButton; | |
@property (weak, nonatomic) IBOutlet UIButton *playButton; | |
@end | |
@implementation RCViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self.stopButton setEnabled:NO]; | |
[self.playButton setEnabled:NO]; | |
AVAudioSession *session = [AVAudioSession sharedInstance]; | |
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; | |
[self setup]; | |
} | |
- (IBAction)recordButtonTapped:(id)sender | |
{ | |
if (self.audioPlayer.isPlaying) { | |
[self.audioPlayer pause]; | |
} | |
if ( ! self.audioRecorder.recording) { | |
AVAudioSession *session = [AVAudioSession sharedInstance]; | |
[session setActive:YES error:nil]; | |
// Start recording | |
[self.audioRecorder record]; | |
[self.recordButton setTitle:@"Pause" forState:UIControlStateNormal]; | |
} else { | |
// Pause recording | |
[self.audioRecorder pause]; | |
[self.recordButton setTitle:@"Record" forState:UIControlStateNormal]; | |
} | |
[self.stopButton setEnabled:YES]; | |
[self.playButton setEnabled:NO]; | |
} | |
- (IBAction)stopButtonTapped:(id)sender | |
{ | |
[self.audioRecorder stop]; | |
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; | |
[audioSession setActive:NO error:nil]; | |
} | |
- (IBAction)playButtonTapped:(id)sender | |
{ | |
NSError *error; | |
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.audioRecorder.url error:&error]; | |
self.audioPlayer.delegate = self; | |
if ( ! self.audioRecorder.recording) { | |
[self.audioPlayer play]; | |
} | |
} | |
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag | |
{ | |
[self.recordButton setTitle:@"Record" forState:UIControlStateNormal]; | |
[self.stopButton setEnabled:NO]; | |
[self.playButton setEnabled:YES]; | |
} | |
- (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]; | |
} | |
- (void)setup | |
{ | |
NSArray *pathComponents = [NSArray arrayWithObjects: | |
[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject], | |
@"MyAudioMemo.m4a", nil]; | |
NSURL *outputFileUrl = [NSURL fileURLWithPathComponents:pathComponents]; | |
NSMutableDictionary *recordSettings = [NSMutableDictionary dictionary]; | |
[recordSettings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKeyPath:AVFormatIDKey]; | |
[recordSettings setValue:[NSNumber numberWithFloat:44100.0] forKeyPath:AVSampleRateKey]; | |
[recordSettings setValue:[NSNumber numberWithInt:2] forKeyPath:AVNumberOfChannelsKey]; | |
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileUrl settings:recordSettings error:nil]; | |
self.audioRecorder.delegate = self; | |
self.audioRecorder.meteringEnabled = YES; | |
[self.audioRecorder prepareToRecord]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment