我正在尝试编写一个简单的iOS
程序,该程序会在插入/拔出耳机时发出通知。我对Objective-C
and很陌生iOS
,很多代码是由其他人编写的,我现在正在尝试调整它,所以我在解决这个问题时遇到了问题。代码在这里:
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
static void onHeadsetChange(void *, AudioServicesPropertyID, UInt32, const void *);
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.headsetOnHandler = self.headsetOffHandler = ^{};
NSLog(@"%lu", AudioSessionInitialize(NULL, NULL, NULL, NULL));
NSLog(@"%lu", AudioSessionSetActive(true));
NSLog(@"%lu", AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, &onHeadsetChange, (__bridge void *)self));
return YES;
}
static void onHeadsetChange(void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) {
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
NSLog(@"The headset changed");
AppDelegate *const delegate = (__bridge AppDelegate *)inUserData;
const CFDictionaryRef routeChangeDictionary = inPropertyValue;
const CFNumberRef routeChangeReasonRef = CFDictionaryGetValue(routeChangeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue(routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
switch (routeChangeReason) {
case kAudioSessionRouteChangeReason_NewDeviceAvailable:
//NSLog([delegate description]);
[delegate headsetOnHandler];
break;
case kAudioSessionRouteChangeReason_OldDeviceUnavailable:
[delegate headsetOffHandler];
break;
default:
break;
}
}
@end
视图控制器.m
#import "ViewController.h"
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
delegate.headsetOnHandler = ^{ self.view.backgroundColor = [UIColor greenColor];};
delegate.headsetOffHandler = ^{ self.view.backgroundColor = [UIColor redColor];};
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我得到EXC_BAD_ACCESS error, code=1 at [delegate headsetOnHandler] and [delegate headsetOffHandler]
. 如果我需要包含这些.h
文件以使其有意义,我会的,如果我还缺少其他任何东西,请告诉我。
编辑:我添加了人们认为应该添加的声明:
@property (readwrite, assign) void (^headsetOnHandler)(void);
@property (readwrite, assign) void (^headsetOffHandler)(void);