Couldn't hear AVAudioRecorder's voice without headphones - iphone

I am recording voice on iPhone using AVAudioRecorder and these are my recorder settings:
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
The problem is that I can't hear the recorded voice without headphones.
I want to be able to hear the voice without headphones also.
How should I change my code?

You have to set the AVAudiosession category to AVAudioSessionCategoryPlayback before playing the sound

Another thing to consider is that if you do not set up your AVAudioSession for playback, then the silence ringer switch on the side of the phone will make your audio not play over the speaker. In order for you to be able to play sound with a silenced phone make sure you are using something like this before triggering your AVAudioPlayer to play the file:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];

Maybe force-routing the audio output to the speaker will help?
- (void) forceRouteAudioToSpeaker
{
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,
sizeof(audioRouteOverride), &audioRouteOverride);
}
Don’t forget to include <AudioToolbox/AudioServices.h>. On second thought, switching the audio category to plain playback should also do the trick, and you say it didn’t…

Related

Different AVAudioSession needed for Recording and Playing in IOS

I am continuing this question from my old post. I read much content for audio recording and playing using Audio queues, audio bufferes, Unfortunately due to less time I compromised and planning to use AVFondation frame work for recording and playing of recorder audio.
My idea: creating one instance of AVRecorder and record through micro phone, same audio planning to play through Iphone speaker using AVAudioPlayer (I ll use multiple instances)
Please bare my tons of lines for recording and playing
-(id)startAudioRecorder:(NSUInteger)viewTag {
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
//Set the general audio session category
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryErr];
//Make the default sound route for the session be to use the speaker
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);
//Activate the customized audio session
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
self.audioRecorder=nil;
//AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
//[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init] ;
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];//kAudioFormatAppleIMA4,kAudioFormatAAC
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
NSString *absolutePath=[self getAbsoluteAudioFilePath:viewTag];
NSError *audioRecorderError=nil;
NSURL *absoluteUrl=[NSURL fileURLWithPath:absolutePath];
self.audioRecorder=[[[AVAudioRecorder alloc] initWithURL:absoluteUrl settings:recordSetting error:&audioRecorderError] autorelease];
NSLog(#"%s audioRecorder created=%#",__func__,self.audioRecorder);
BOOL isDurationAccepted=[self.audioRecorder recordForDuration:1800.0];
BOOL record=[self.audioRecorder record];
[recordSetting release];
recordSetting=nil;
//if(error !=NULL) *error=audioRecorderError;
return nil;
}
-(id)playRecordedAudio:(NSUInteger)viewTag
{
NSURL *absoluteUrl=[NSURL fileURLWithPath:[[self getAbsoluteAudioFilePath:viewTag] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]];
NSError *audioPlayerError=nil;
recordingView.audioPlayer=[[[AVAudioPlayer alloc] initWithContentsOfURL: absoluteUrl error:&audioPlayerError] autorelease] ;
recordingView.audioPlayer.delegate=self;
[recordingView.audioPlayer prepareToPlay];
recordingView.audioPlayer.numberOfLoops=-1;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[recordingView.audioPlayer play];
return nil;
}
The above code perfectly working as I expected. But when I am looking for recording->playing, again recording(before finishing currently playing audio) this time currently playing audio coming to microphone(ear phones) instead of playing through speakers.
Now the problem is, I am unable to record and play simultaneously. I am expecting record(even audio playing) always through earphones and multiple audios play through phone speaker.
Please suggest me changes in my above code to come out of this situation. All your comments helpful.
Set AVAudioSessionCategoryPlayAndRecord for playing those recording

Playing video with AVPlayer and recording sound with AVAudioRecorder simultaneously

I was googling this problem for many days but I didn't find a solution for my simple problem.
I'm trying to play a video while recording sound. Recording sound works fine without the video. And the video works fine without the recording.
As soon as I put both together, the recording throws values which do not react on sound and the video is not playing anymore.
By the way this happens only on my device (iPhone 4 / iOS5). In the simulator everything works fine.
This is a reduced version of my code which is in a ViewController.
// Audio Recorder
NSURL *nullUrl = [NSURL fileURLWithPath:#"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
[NSNumber numberWithInt:kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt:1], AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *recorderError;
_recorder = [[AVAudioRecorder alloc] initWithURL:nullUrl settings:settings error:&recorderError];
if (_recorder)
{
[_recorder prepareToRecord];
_recorder.meteringEnabled = YES;
[_recorder record];
_levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(levelTimerCallback:) userInfo:nil repeats:YES];
}
// Video Player
NSURL *url = [[NSBundle mainBundle] URLForResource:#"Video"
withExtension:#"mp4"
subdirectory:nil];
_avPlayer = [[AVPlayer playerWithURL:url] retain];
_avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:_avPlayer] retain];
_avPlayerLayer.frame = self.view.layer.bounds;
[self.view.layer addSublayer:_avPlayerLayer];
[_avPlayer play];
The recorder simply outputs a value which is read in the levelTimerCallback method.
The video simply plays a short video.
I figured out that I have to set a category for the AVAudioSession.
If I implement this code after app start, the video is playing but the recorder still outputs the same values without reacting to sound.
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioSession setActive:YES error:&error];
I could imagine that I might have to do something with AudioSessionSetProperty
UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
But that doesn't change anything.
Do you have an idea what I am missing or how to set up the AudioSession properly?
Thank you for every hint on that!
Cheers,
Raphael ;)
I had the same problem (except I'm using the AVPlayer to play audio and not video though). I suggest you start listening for audio routing changes and you will see the route change multiple times when the AVPlayer and AVAudioRecorder are started at the same time.
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self);
In the callback print out the category and route (kAudioSessionProperty_AudioRoute). The only way I could get the audio playback and record to work simultaneously was if:
The audio session was AVAudioSessionCategoryPlayAndRecord and kAudioSessionProperty_OverrideCategoryMixWithOthers property was true.
I initiated the playback before the recording. After AVAudioRecorder prepareToRecord is called, don't start recording until you get the audio route change. Even though it does not fail if you start recording earlier, the audio route change seems to make the recording fail.
I tried other combinations, but this is the only one that works every time.

Record and play audio same time

I am trying to make the audio record and play back at the same time. Here is the code. It will only record and play back later. I want it to play back simultaneously over the speakers. Any help. I am not even sure I am using the right classes.
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithString:#"VoiceFile"]]];
recorder = [[AVAudioRecorder alloc] initWithURL:temporaryRecFile settings:nil error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];
player.volume = 1;
[player play];
Are you recording from the iPhone microphone? Chances are you'll get feedback if you try to play the recording back while recording it. The microphone will hear what the speakers are playing and rerecord it at a slight delay. This would snowball until your recording sounds like a train wreck.
You are using correct classes but I think you must add AutioUnits also.
Apple has a sample code aurioTouch2 and I think it contains what you need.

AVAudioSession reroutes iPhone audio automatically?

I'm having problems with AVAudioSession using the AVAudioRecorder in a cocos2d game that I'm working on.
I'm trying to capture mic input using a simple AVAudioRecorder example to detect when the user makes a sound in the mic (the sound itself doesn't matter, as I'm recording into /dev/null).
Here is my setup code for the microphone:
NSURL *newURL = [[NSURL alloc] initFileURLWithPath:#"/dev/null"];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 22050.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityLow],
AVEncoderAudioQualityKey,
nil];
micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil];
[newURL release];
[recordSettings release];
[micInput setMeteringEnabled:YES];
On the iPhone with the above code, the scene starts with all of the audio (sound effects, background music, etc.) playing at a really low level, because it is only playing through the phone speaker instead of the external speaker. When I test this on iPad or iPod Touch, the background audio plays through the external speaker as expected. This is a problem, since the volume of the game lowers drastically when playing on the iPhone version during this particular part of the game.
When I comment out the AVAudioSession setup line, the sounds play through the external speaker, but of course I can't get microphone input anymore. Is there any workaround or solution to this problem? I need to be able to record with AVAudioRecorder but still have audio output from the iPhone's external speaker.
Thanks!
Try something like the following after you setup your audio session:
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),
&ASRoute
);

How to use MPMusicPlayerController with AVAudioPlayer

I am trying to play an in app audio with ipod the code I am using to play the audio file is:
Code:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if(error)
{
NSLog(#"Some error happened");
}
NSString *path = [[NSBundle mainBundle] pathForResource:effect ofType:type];
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
myPlayer.delegate = self;
myPlayer.numberOfLoops = -2;
myPlayer.volume = 1.0f;
[myPlayer play];
and to play the ipod music I am using
Code:
player = [MPMusicPlayerController iPodMusicPlayer];
My ipod plays fine until the audio starts playing but once the audio stops I am not able to get back to the ipod to play. I am using
Code:
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:#"sound"];
if([value compare:#"ON"] == NSOrderedSame && myPlayer != nil)
{
[myPlayer stop];
[myPlayer release];
myPlayer = nil;
}
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if(error)
{
NSLog(#"Some error happened");
}
to stop the audio and then just call
Code:
[player play];
to play the ipod music but it does not play the music, it says the title to be null.
I would really appreciate if some one could help me with this.
Regards,
Ankur
I suggest moving the audio session setup (including the Ambient category) to your application delegate's didFinishLaunchingWithOptions and not using the Playback category at all. You can still play AVAudio sounds just fine with the Ambient setting.
If you want to kill iPod music whenever you play AV sounds, then you should only be using the Playback category. (I'm not exactly sure what you're trying to do.)
The error that you're getting when you try to resume the iPod music suggests that your MPMusicPlayerController object resets its song queue when it is stopped by the AVAudio sounds, and that you'll have to set up the MP player again. "Title is null" definitely sounds like a dangling pointer or the like in the MP player.
It's not possible to play iPod audio in AVAudioPlayer. You need to convert the library Asset link from ipod, using *AVURLAsset to an mp3 or wav and save it to the documents library. IOS.5 currently has a bug in the conversion (AudioExportSession) from ipod to mp3 file, so it's NOT POSSIBLE. Sorry.
You need to resume iPod player after myPlayer finish playing.
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[player play];
}