Created
June 14, 2012 22:04
-
-
Save arielelkin/2933251 to your computer and use it in GitHub Desktop.
SimpleAudioRecording
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
// | |
// Recorder.h | |
// | |
// Created by Ariel Elkin on 14/05/2012. | |
// | |
#import <Foundation/Foundation.h> | |
#import <AVFoundation/AVFoundation.h> | |
@interface NarrationManager : NSObject | |
-(void)startRecording:(int)page; | |
-(void)stopRecording; | |
-(void)playRecording:(int)page; | |
-(void)stopPlaying; | |
@end |
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
// | |
// Recorder.m | |
// | |
// Created by Ariel Elkin on 14/05/2012. | |
// | |
#import "NarrationManager.h" | |
@implementation NarrationManager | |
AVAudioRecorder *recorder = nil; | |
AVAudioPlayer *player = nil; | |
-(void)startRecording:(int)i { | |
NSLog(@"startRecording"); | |
recorder = nil; | |
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; | |
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; | |
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10]; | |
[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey]; | |
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey]; | |
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; | |
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; | |
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; | |
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; | |
NSString *urlString = [[NSHomeDirectory() stringByAppendingString:@"/Documents/"] stringByAppendingString:@"recordTest.caf"]; | |
NSURL *url = [NSURL fileURLWithPath:urlString]; | |
NSLog(@"Created url at %@", url); | |
NSError *error = nil; | |
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error]; | |
bool b = [recorder prepareToRecord]; | |
if(!b){ | |
NSLog(@"can't prepare recording"); | |
return; | |
} | |
if(error != nil) { | |
NSLog(@"Error: %@", error); | |
return; | |
} | |
else { | |
[recorder record]; | |
NSLog(@"recording"); | |
} | |
} | |
-(void)stopRecording { | |
if(recorder.recording){ | |
[recorder stop]; | |
NSLog(@"stopped recording"); | |
} | |
else{ | |
NSLog(@"recorder not recording"); | |
} | |
} | |
-(void) playRecording:(int) page { | |
NSLog(@"playRecording"); | |
// Init audio with playback capability | |
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; | |
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; | |
NSString *urlString = [[NSHomeDirectory() stringByAppendingString:@"/Documents/"] stringByAppendingString:@"recordTest.caf"]; | |
NSURL *url = [NSURL fileURLWithPath:urlString]; | |
NSLog(@"trying to play %@", url); | |
NSError *error; | |
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; | |
if(error != nil){ | |
NSLog(@"PLAY ERROR: %@", error); | |
return; | |
} else{ | |
player.numberOfLoops = 0; | |
[player play]; | |
NSLog(@"playing"); | |
} | |
} | |
-(void)stopPlaying{ | |
[player stop]; | |
NSLog(@"stopped"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment