There is an interactive movie on ios device. when movie starts (tap), the guy at the start of video will ask you plug headset , if plugged, then video should automatically jump straight to the story(straight go to the video-story). what should i do? and how to write a code?
Asked
Active
Viewed 4,930 times
2
-
http://i48.tinypic.com/1g3lgl.jpg ignore blur part on the pic – user1369476 May 21 '12 at 12:33
-
1Refer [this](http://stackoverflow.com/questions/3575463/detecting-if-headphones-are-plugged-into-iphone) link . It will help you – Paresh Navadiya May 21 '12 at 12:33
-
how do register for AudoRoute Changes? – user1369476 May 29 '12 at 09:39
3 Answers
3
First you will have to register for AudioRoute Changes :-
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self);
Here You can depict the reason for changing your route :-
CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef =
CFDictionaryGetValue (routeChangeDictionary,
CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
// your statements for headset unplugged
}
if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
// your statements for headset plugged
}

Abhishek Singh
- 6,068
- 1
- 23
- 25
-
what if a user will start(tap) with already plugged headset? does that means setting will be set by default? i mean, can i write [routeChangeReason == kAudioSessionRouteChangeReason_HeadsetUnavailable]...? – user1369476 May 24 '12 at 03:20
-
routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable I already gave statement this statement for plugged in headset. – Abhishek Singh May 24 '12 at 04:08
-
thanks, but i meant by this algorithm: if originally unplugged then play A after 5 seconds play B(because might be somebody doesnt have a headset), if headset plugged as asked above, then straight play B. if headset originally plugged before taping on video, then straight play B. if unplugged during playing, then go to the external speakers. – user1369476 May 24 '12 at 07:06
-
0
This could be an other way:
CFStringRef newRoute;
size = sizeof(CFStringRef);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), "couldn't get new audio route");
if (newRoute)
{
CFShow(newRoute);
if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == kCFCompareEqualTo) // headset plugged in
{
...
}
else if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), NULL) == kCFCompareEqualTo){
....
}
}

Balazs Nemeth
- 2,333
- 19
- 29
0
First check if the device is connected to any headset.
+(BOOL)isHeadsetPluggedIn {
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
return YES;
}
return NO;
}
Then based on the bool value, write your own conditions. Something like below..
if (isHeadphonesConnected) {
//Write your own code here
}else{
}
Also you can register a notification incase you want to know if the headset is removed when you are in the screen.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioRoutingListenerCallback:)
name:AVAudioSessionRouteChangeNotification
object:nil];
- (void)audioRoutingListenerCallback:(NSNotification*)notification
{
NSDictionary *interuptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch (routeChangeReason) {
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
NSLog(@"Headphone/Line plugged in");
/*Write your own condition.*/
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
NSLog(@"Headphone/Line was pulled.");
/*Write your own condition.*/
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
// called at start - also when other audio wants to play
break;
}
}

Pradeep Reddy Kypa
- 3,992
- 7
- 57
- 75