English 中文(简体)
电话插播会
原标题:headphone plug-in plug-out event when audio route doesn t change - iOS

I m working on iPad. I would like to detect when user plug-out headphone. First I used a listener on the property kAudioSessionProperty_AudioRouteChange. So all was working well until I ve decided to add a button to switch to speakers when headphone was still plugged. So I’m now facing a problem, maybe someone would have an idea to fix it.

这里的情况是:

  • I plug a headphone -> my audio route change callback is called
  • then I switch sound to speakers (without unplugging my headphone) -> audio route change callback is called
  • then I unplug my headphone (when sound is still outputting to speakers) -> audio route change callback is NOT called, which seems logical.

但我的问题是: 因此,我的问题是: 你们是否看到了一种办法,可以发现这名头盔没有被打上最后一件事。

感谢您的帮助

http://www.un.org。

Ok I发现了一个工作:

为了发现耳光是否被封不动,我在我需要知道的任何时候(而不是使用ean子)都履行测试职能,这或许对业绩不太好,但对于工作来说却不太好。

//set back the default audio route
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

//check if this default audio route is Heaphone or Speaker
CFStringRef newAudioRoute;
UInt32 newAudioRouteSize = sizeof(newAudioRoute);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &newAudioRouteSize, &newAudioRoute);

NSString *newAudioRouteString = (__bridge NSString *)newAudioRoute;

CFRelease(newAudioRoute);

//if this default audio route is not Headphone, it means no headphone is plugged
if ([newAudioRouteString rangeOfString:@"Headphones"].location != NSNotFound){
    NSLog(@"Earphone available");
    return true;
}
else {
    NSLog(@"No Earphone available");
    return false;
}

希望能帮助人们!

问题回答

I imagine a solution it in the following way: You create in the AppDelegate a boolean for the speakers, let s say: BOOL isSpeakerOn. And every time the audio route callback is called you have to verify what the current situation with the speakers and what you want to do then.

这是处理这个问题的最佳理论:





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签