iPhone Developer\'s Cookbook, 3.0 Edition
BSD License, Use at your own risk
*/
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import "ModalAlert.h"
#define COOKBOOK_PURPLE_COLOR[UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR)[[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define SYSBARBUTTON(ITEM, TARGET, SELECTOR) [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:ITEM target:TARGET action:SELECTOR] autorelease]
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
#define FILEPATH [DOCUMENTS_FOLDER stringByAppendingPathComponent:[self dateString]]
#define XMAX20.0f
@interface TestBedViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
{
AVAudioRecorder *recorder;
AVAudioSession *session;
IBOutletUIProgressView *meter1;
IBOutletUIProgressView *meter2;
NSTimer *timer;
}
@property (retain) AVAudioSession *session;
@property (retain) AVAudioRecorder *recorder;
@end
@implementation TestBedViewController
@synthesize session;
@synthesize recorder;
/*
-(IBAction)start:(id)sender{
recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
soundsDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"Sounds"];
[[NSFileManager defaultManager] createDirectoryAtPath:soundsDirectoryPath withIntermediateDirectories:YES attributes:nil error:NULL];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Test.m4a", [soundsDirectoryPath retain]]];
NSLog(@"Path : %@", [url absoluteString]);
NSError *err = nil;
if([recorder isRecording])
{
[recorder stop];
recorder = nil;
}
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
[recorder setDelegate:self];
if(err)
NSLog(@"ERROR : %@", err);
BOOL st = [recorder prepareToRecord];
if(!st){
NSLog(@"Failed");
}
recorder.meteringEnabled = YES;
BOOL status = [recorder record];
if(!status)
NSLog(@"Failed");
}
-(IBAction)stop:(id)sender{
if([recorder isRecording]){
NSLog(@"Recording...");
}else{
NSLog(@"Not recording...");
}
[recorder stop];
}
-(void) audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Test.m4a", [soundsDirectoryPath retain]]];
NSError *err = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
[player setDelegate:self];
[player setMeteringEnabled:YES];
[player play];
}
*/
- (NSString *) dateString
{
// return a formatted string for a file name
NSDateFormatter *formatter = [[[NSDateFormatteralloc] init] autorelease];
formatter.dateFormat = @"ddMMMYY_hhmmssa";
return [[formatter stringFromDate:[NSDatedate]] stringByAppendingString:@".m4a"];
}
- (NSString *) formatTime: (int) num
{
// return a formatted ellapsed time string
int secs = num % 60;
int min = num / 60;
if (num < 60) return [NSString stringWithFormat:@"0:%02d", num];
return[NSString stringWithFormat:@"%d:%02d", min, secs];
}
- (void) updateMeters
{
// Show the current power levels
[self.recorderupdateMeters];
float avg = [self.recorderaveragePowerForChannel:0];
float peak = [self.recorderpeakPowerForChannel:0];
meter1.progress = (XMAX + avg) / XMAX;
meter2.progress = (XMAX + peak) / XMAX;
// Update the current recording time