0

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;
        }
    }
}

}
Sargis
  • 1,196
  • 1
  • 18
  • 31
FreeAppl3
  • 858
  • 1
  • 15
  • 32
  • “am pretty certain it is due to the retain count or the system releasing the object immediately” Could be but have you walked thru the code with the debugger and the variables list to see what’s happening? If `initWithContentsOfURL` fails you’d get a nil player. No need to guess, use the debugger. – matt Jan 30 '20 at 04:55

1 Answers1

1

If there is no strong reference keeping hold of it, your AudioSession will go away as soon as the method returns.

gnasher729
  • 51,477
  • 5
  • 75
  • 98