Note that there are some explanatory texts on larger screens.

plurals
  1. POSounds on multiple buttons and stop when others play in iOS
    primarykey
    data
    text
    <p>I'm trying to put together a little app to kind of learn some basics of iOS development so forgive me if this is basic. I've looked at quite a few threads but I've got a couple questions that are specific to this app I think. </p> <ol> <li><p>I've seen a couple different ways to play audio when a button is pressed. One involves the "CFBundleRef" (<a href="https://stackoverflow.com/questions/884452/how-to-play-iphone-tap-sound">How to play iPhone tap sound?</a>) and the other "AVAudioPlayer" (<a href="https://stackoverflow.com/questions/11525942/play-audio-ios-objective-c">Play Audio iOS Objective-C</a>). How do I know which to use and when to use each appropriately? </p></li> <li><p>I've got the audio working currently using the first option, but I can't seem to figure out how to get the sound to stop when another button is pressed. How can I do this?</p></li> <li><p>Is there an easier way to link 15 sounds / buttons together other than what I've done? </p></li> </ol> <p>Here's the .m file:</p> <pre><code>#import "animalsViewController.h" @interface animalsViewController () @end @implementation animalsViewController -(IBAction) boom; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom2; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird2", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom3; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird3", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom4; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chicken1", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom5; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chicken2", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom6; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"cow", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom7; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"dog1", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom8; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"duck", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom9; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"horse", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom10; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"lion", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom11; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"monkey", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom12; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"owl", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom13; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"pig", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom14; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"rooster", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } -(IBAction) boom15; { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sheep", CFSTR ("mp3"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &amp;soundID); AudioServicesPlaySystemSound(soundID); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end </code></pre> <p>And here's the .h file:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;AudioToolbox/AudioToolbox.h&gt; @interface animalsViewController : UIViewController{ } -(IBAction) boom; -(IBAction) boom2; -(IBAction) boom3; -(IBAction) boom4; -(IBAction) boom5; -(IBAction) boom6; -(IBAction) boom7; -(IBAction) boom8; -(IBAction) boom9; -(IBAction) boom10; -(IBAction) boom11; -(IBAction) boom12; -(IBAction) boom13; -(IBAction) boom14; -(IBAction) boom15; @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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