Recording and playing audio with iPod - iphone

I'm trying to record and play audio in an universal app. The AVAudioSession configuration is:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 category = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
This is working fine for devices with built-in microphone. But in a third generation iPod I am experiencing random undesired behaviours. Sometimes all works fine, and sometimes it happens only when the earphones with the integrated mic are plugged-in. In this case, is not possible to hear any sound in the app without the earphones.

The 3rd generation of iPod Touch doesn't have a built-in microphone. The only way to have one is to plug your headphones with a mic.
The only generation of iPod Touch with a built-in microphone is the 4th.
You can check if inputRecording is available :
UInt32 propertySize, micConnected;
AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);

Related

unable to get the Audio output in iOS

I have a codebase which plays audio and video perfectly. I have converted that into a small framework and started using it as a part of another code. When i do this, there is no audio output.... I am unable to hear any audio output while it is being played. All the Audio player delegate methods are being called but there is no sound. The same is happening with Videos as well. All the videos are being played with out sound. Its like watching a silent movie.... Any ideas? I am using AVAudioPlayer for playing audio files and MPMoviePlayerController for playing videos.
edit : This happens only on device. It works all fine in simulator
Just came across this post, I was doing the same thing, and actually AVAduioSession is needed to play audio thru speaker/headset. So having AVAudioSession is a must.
//REALLY NEED THIS LINE FOR MUSIC TO PLAY TO SPEAKER
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
and for background playing, add these 2 lines and change setting in plist to allow background mode
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
So stupid of me.... The problem was with the volume button on iPad. The volume button in iPad with iOS 5 has some weird bug. It does not update itself when we toggle the button between screen lock and audio output. I guess this is bug from apple and filed a bug. Hope they solve this in their next update.

Checking Microphone Volume in iPhone

How to check whether microphone is muted before recording audio in iPhone?
Actually, you don't have to. You have to "warn" AVAudioSession that you are going to record a bit of Audio. Doing so, even if you are muted (on the IPhone, I Have not tested this with any external device) you will be able to record your audio... here is the piece of code that does that:
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryRecord
error: &error];
it even records with a higher volume.
Best of luck...

AVAudioSession category not working as documentation dictates

I have an iOS app that has some audio feedback in certain places, but I want any other music the user has playing in the background to be allowed to play over this. In addition, I want the audio in my app to respect the mute switch. According to the developer documentation, this functionality should all be enabled by the AVAudioSession ambient category. This is the code I'm using:
if (!hasInitialisedAudioSession) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:NULL];
[session setActive:YES error:NULL];
hasInitialisedAudioSession = YES;
}
The code is executing just fine, and it does indeed let the app sounds play over iPod music. What it doesn't do, however, is respect the mute switch. I've tried swapping this code out for similar C audio calls (stuff like AudioSessionSetProperty) instead of the Objective-C calls, but I get the same result - the ambient session category simply doesn't want to respect the mute switch, despite what the documentation says it should be doing.
Any ideas? Thanks for the help :)
I think I managed to work it out - turns out that it has nothing to do with my app at all, but rather the iPod app. My app obeys the mute switch as it should when the iPod isn't playing, and then allows the iPod to play over it - all behaviour I wanted. However, when the iPod is playing, the app stops responding to the mute switch, so I think it's just something the iPod does to the device audio settings. I could probably work a way around it if I really wanted to spend the time on it, but as long as it obeys the mute switch when the iPod isn't playing that's good enough for me.
EDIT: to work around this, just use this function to determine whether or not the mute switch is on manually, and don't play your sounds if the result is YES. Could be a bit of a pain if you don't have a central audio manager class, though. It would be nice if Apple could publish this behaviour in their documentation.
- (BOOL)deviceIsSilenced
{
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) <= 0);
}

iPhone AudioSession properties

I'm developing an app that should have the following properties regarding the audio:
can record and play sound at the same time
can mix the audio output with other app, e.g. iPod
audio output at speaker when earphone is not plugged in
audio output at earphone when it is plugged in
I used the following code.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *audioSessionError;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&audioSessionError];
UInt32 mix = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(mix), &mix);
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
[audioSession setActive:YES error:&audioSessionError];
However, I can achieve 1-3 but failed at 4. When earphone is plugged in, the audio still comes through the speaker. Then I tried setting kAudioSessionProperty_OverrideCategoryDefaultToSpeaker instead of kAudioSessionProperty_OverrideAudioRoute, but this resulted pausing the iPod instead of mixing both audio. Could anyone please help pointing out what's wrong with the above code?
Thanks for any help.
I think this:
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
Specifically means use the speaker. The default action (to use the headphones when plugged in) should be:
UInt32 route = kAudioSessionOverrideAudioRoute_None;

iPhone: How to detect if iTunes is playing?

I noticed that some apps programmatically mute itunes (if its running) at launching. How is this achieved? I have a game with background music and would like to either stop itunes or get at least a message that itunes is playing so that I can stop the game's background music.
thx,
marc.
You don't need to. With Audio Session you can decide how the audio should behave.
From the Audio Session Programming Guide:
With the audio session interface, you
specify aspects of your application’s
audio behavior and configure it to
live harmoniously within the iPhone
audio environment. You start by asking
yourself questions such as these:
Do you want your audio to be silenced by the Ring/Silent switch?
The answer is probably “yes” if audio
is not essential to using your
application successfully. (Users will
appreciate being able to run your game
in a meeting with no one the wiser.)
Do you want iPod audio to continue playing when your audio
starts? This could be appropriate for
a virtual piano, letting users play
along to songs in their libraries.
You’d want iPod audio to stop,
however, for a streaming radio
application.
You probably want this:
UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty (
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);
For more behaviour types, check the Audio Session Categories, or read the entire Audio Session Programming Guide.
I had the opposite problem. My app plays a short video with no sound after launch. This caused the iTunes music playing in the background to mute.
In order to keep the music playing, I add this in the applicationDidFinishLaunching:
NSError* error;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];
if (error) NSLog(#"Unable to configure Audio");