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?
Detecting if headset are plugged into iOS device
4.9k views Asked by user1369476 At
3
There are 3 answers
0
On
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){
....
}
}
0
On
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;
}
}
First you will have to register for AudioRoute Changes :-
Here You can depict the reason for changing your route :-