I recently updated my project with Automatic Reference Counting (ARC)
and it broke my playAudio methods (worked perfect before). I am new to ARC
but am pretty certain it is due to the retain count or the system releasing the object immediately so you never hear the sound play at all. I create a iVar of the AudioPlayer
and try to keep my own personal retain count as found in question linked below. I call out the playSystemSound:
from another class, by first alloc]init; then [class playSystemSound:1002];
If anyone can point out what it is I am doing wrong that would be greatly appreciated.
AVAudioPlayer not playing any sound
@property (strong, nonatomic) AVAudioPlayer *soundToPlay;
-(void)playsystemSound:(long long)eventType{
NSString *path = [NSString stringWithFormat:@"/User/Library/Preferences/com.idd.iconomaticprefs.plist"];
NSMutableDictionary* savedDict = [NSMutableDictionary dictionary];
[savedDict addEntriesFromDictionary:[NSDictionary dictionaryWithContentsOfFile:path]];
NSMutableArray *activeThemesArray = [savedDict valueForKey:@"ActiveThemes"];
if ([activeThemesArray count]) {
NSString *soundURL;
for (NSString *name in activeThemesArray) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds",name];
if ([[NSFileManager defaultManager]fileExistsAtPath:soundURL]) {
//UISounds avilable in theme determine event type
if (eventType == 1002) {
//respring?
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/lock.caf",name];
}
if (eventType == 1003) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/Anticipate.caf",name];
}
if (eventType == 1004) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/key_press_click.caf",name];
}
if (eventType == 1008) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/photoShutter.caf",name];
}
// setup session correctly
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audiosession setActive:YES error:nil];
// play sound
NSURL *url = [NSURL fileURLWithPath:soundURL];
self.soundToPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.soundToPlay prepareToPlay];
[self.soundToPlay play];
break;
}
}
}
}