Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As bneely said. You need to create an instance variable (or ivar) so that multiple methods can reference the same variable. And as you already know <code>AudioServicesDisposeSystemSoundID()</code> stops the currently playing <code>SystemSoundID</code>. And once you declare an ivar you don't need to declare the variable in each method. So the logical conclusion would be.</p> <pre><code>@implementation ViewControllerClassNameHere{ SystemSoundID soundID; } - (IBAction)beyond:(id)sender { AudioServicesDisposeSystemSoundID(soundID); CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"beyond" ,CFSTR ("wav") , NULL); AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } - (IBAction)Years:(id)sender { AudioServicesDisposeSystemSoundID(soundID); CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"years" ,CFSTR ("wav") , NULL); AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } </code></pre> <p>You also might (will definitely) want to <code>CFRelease()</code> the <code>CFURLRef</code>s you copy once you've created the <code>SystemSoundID</code>(so you don't leak memory). In fact you might consider making your <code>CFURLRef</code>s ivars so as not to constantly copy them. You also likely want to call <code>AudioServicesDisposeSystemSoundID(soundID);</code> in <code>dealloc</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload