Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// check existence of a local file within an app bundle -------------------------------------
NSString *soundFile = @"sound.wav";
NSFileManager* manager;
manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:soundFile]])
printf("file Exists...\n\n");
else
printf("File not exist...\n\n");
// Load an AVAudioPlayer --------------------------------------------------------------------
NSError* err = nil;
self.beepCountdown = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"beep-2.wav"]] error:err];
if( err ) NSLog(@"Failed to load audio because: %@", [err localizedDescription]);
// play it
self.beepCountdown.delegate = self;
self.beepCountdown.numberOfLoops = 0;
[self.beepCountdown prepareToPlay];
[self.beepCountdown play];
// Make AVAudioPlayer play nice with video recording ----------------------------------------
// from: http://stackoverflow.com/questions/11785767/avaudioplayer-breaking-video-capture?rq=1
- (void)setupAudioSession {
static BOOL audioSessionSetup = NO;
if (audioSessionSetup) return;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
audioSessionSetup = YES;
}
// route all audio through speakers - disregard devices plugged into headhpone jack -----------
- (void)setupAudioSession
{
// Called once to route all audio through speakers, even if something's plugged into the headphone jack
static BOOL audioSessionSetup = NO;
if (audioSessionSetup) return;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
// set category to accept properties assigned below
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: &sessionError]; // withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
// Force audio to come out of speaker
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
// fix issue with audio interrupting video recording - allow audio to mix on top of other media
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
// set flag
audioSessionSetup = YES;
}
// save and restore a value from disk -------------------------------------------------------------------
// save
[[NSUserDefaults standardUserDefaults] setBool:self.buttonStayLoggedIn.selected forKey:STAY_LOGGED_IN_PREF_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
// restore
BOOL storedValue = [[NSUserDefaults standardUserDefaults] boolForKey:STAY_LOGGED_IN_PREF_KEY];
if(storedValue == YES) self.buttonStayLoggedIn.selected = YES;
// add a nice rounded border to a UITextField -----------------------------------------------------------
self.inputPassword.borderStyle = UITextBorderStyleNone;
self.inputPassword.layer.cornerRadius = 4.0f;
self.inputPassword.layer.borderColor = [UIColor colorWithRed:217/255.f green:217/255.f blue:217/255.f alpha:1].CGColor;
self.inputPassword.layer.borderWidth = 1.0f;
// force a view update (maybe?) -------------------------------------------------------------------------
CFRunLoopRunInMode (CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent()), 0, FALSE);
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
[CATransaction flush];
// backwards-compatible check for iOS 7 -----------------------------------------------------------------
+ (BOOL)isIOS7
{
return ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.