Skip to content

Instantly share code, notes, and snippets.

@arielelkin
Created June 14, 2012 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arielelkin/2933251 to your computer and use it in GitHub Desktop.
Save arielelkin/2933251 to your computer and use it in GitHub Desktop.
SimpleAudioRecording
//
// 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
//
// 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