Question

In a (file:Sound.m / method: setSound) I have this, which successfully sets and plays the sound ( it would seem ).

  [self setTheSound:&thisSoundID];
  AudioServicesPlaySystemSound(*(self.TheSound));

However, when called outside that method. I do not hear anything. Though I know it gets called from the NSLog. ( file:Sound.m / method: playSound )

- (void) playSound
{
    NSLog(@"Called playSound");
    AudioServicesPlaySystemSound(*(self.TheSound));
}

Here is the property setting on ( file:Sound.h / method: playSound )

@property SystemSoundID *TheSound;
Était-ce utile?

La solution

You should not store the address of the sound ID but the sound ID itself in the property (i.e., remove the *):

@property SystemSoundID theSound;

and then use it as

[self setTheSound:thisSoundID];
AudioServicesPlaySystemSound(self.theSound);

In your case, self.theSound is the address of the local variable thisSoundID. As soon as you leave the function where this variable is declared, that memory location might be reused for something else, and referring to it is "undefined behaviour".

(Side note: for instance variables and properties, use names starting with a lowercase letter.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top