Code:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
//NSLog(@"url");
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
//NSLog(@"dizionario");
NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
bool b;
NSLog(@"audio");
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:audioSettings error:&error];
//NSLog(@"audio");
if (recorder) {
NSLog(@"prepara");
b=[recorder prepareToRecord];
NSLog(@"%@",(b ? @"OK" : @"NO"));
NSLog(@"meter");
recorder.meteringEnabled = YES;
NSLog(@"recorder");
b=[recorder record];
NSLog(@"%@", (b ? @"OK" : @"NO"));
// NSLog(@"leveltimer");
// levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else {
NSLog(@"************** ERRORE: ****************");
NSLog([error description]);
}
// Grab a reference to the AVAudioSession singleton
audioSession = [AVAudioSession sharedInstance];
soundCategory = AVAudioSessionCategoryAmbient;
[audioSession setCategory:soundCategory error:&audioSessionError];
// Now we have initialized the sound engine using ambient sound, we can check to see if ipod music is already playing.
// If that is the case then you can leave the sound category as AmbientSound. If ipod music is not playing we can set the
// sound category to SoloAmbientSound so that decoding is done using the hardware.
isExternalAudioPlaying = [self isExternalAudioPlaying];
if (!isExternalAudioPlaying) {
soundCategory =AVAudioSessionCategoryPlayAndRecord;// AVAudioSessionCategoryRecord;//AVAudioSessionCategorySoloAmbient;
audioSessionError = nil;
[audioSession setCategory:soundCategory error:&audioSessionError];
if (audioSessionError)
NSLog(@"WARNING - SoundManager: Error setting the sound category to SoloAmbientSound");
}
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);