我是 Objective c 的新手(从 .NET 开发到 iPhone)。好吧,现在我有一个简单的问题要问有 iPhone 开发经验的人。我正在编写一个静态库,它使用 audiotoolbox 对一些音频事件做出反应。我的标题看起来像:
@interface ccreaderlib : NSObject {
id __unsafe_unretained delegate;
SEL _DevicePluggedEvent;
SEL _DeviceUnpluggedEvent;
}
@property (nonatomic, assign) id delegate;
@property (nonatomic, assign) SEL onDevicePlugged;
@property (nonatomic, assign) SEL onDeviceUnplugged;
- (id)init;
- (void)startMonitor;
- (void)stopMonitor;
@end
现在在 UIViewController 我这样做:
_lib = [[ccreaderlib alloc] init];
_lib.delegate = self;
_lib.onDevicePlugged = @selector(OnDevicePluggedIn);
_lib.onDeviceUnplugged = @selector(OnDeviceUnplugged);
[_lib startMonitor];
我的想法是从我的静态库中调用 UIViewController 的选择器。我怎样才能做到这一点。目前我正在尝试以这种方式执行此操作:
void audioRouteChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueSize,
const void *inPropertyValue)
{
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef =
CFDictionaryGetValue ( routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (
routeChangeReasonRef,
kCFNumberSInt32Type,
&routeChangeReason);
CFStringRef oldRouteRef =
CFDictionaryGetValue (
routeChangeDictionary,
CFSTR (kAudioSession_AudioRouteChangeKey_OldRoute));
NSString *oldRouteString = (__bridge NSString *)oldRouteRef;
ccreaderlib *self = (__bridge id)inUserData;
id reactClass = [self delegate];
if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
if ([oldRouteString isEqualToString:@"Speaker"])
{
[reactClass performSelector:@selector(onDevicePlugged)];
self.bIsReaderPlugged = YES;
}
}
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
if ([oldRouteString isEqualToString:@"Headphone"]){
[reactClass performSelector:@selector(onDeviceUnplugged)];
self.bIsReaderPlugged = NO;
}
}
}
但通过这种方式,我得到了 SIGABRT。请帮助我,已经花了 3 天时间试图解决这个问题。
***更新:自己找到答案。将我的 c 回调函数更改为这样:
if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
if ([oldRouteString isEqualToString:@"Speaker"])
{
if([self.delegate respondsToSelector:self.onDevicePlugged])
[self.delegate performSelector:self.onDevicePlugged];
self.bIsReaderPlugged = YES;
}
}
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
if ([oldRouteString isEqualToString:@"Headphone"]){
if([self.delegate respondsToSelector:self.onDeviceUnplugged])
[self.delegate performSelector:self.onDeviceUnplugged];
self.bIsReaderPlugged = NO;
}
}
感谢http://brandontreb.com/objective-c-programming-tutorial-creating-a-twitter-client-part-1/