0

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.

  1. I've seen a couple different ways to play audio when a button is pressed. One involves the "CFBundleRef" (How to play iPhone tap sound?) and the other "AVAudioPlayer" (Play Audio iOS Objective-C). How do I know which to use and when to use each appropriately?

  2. 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?

  3. Is there an easier way to link 15 sounds / buttons together other than what I've done?

Here's the .m file:

#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, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom2; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird2", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}
-(IBAction) boom3; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird3", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom4; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chicken1", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom5; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chicken2", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom6; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"cow", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom7; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"dog1", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom8; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"duck", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom9; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"horse", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom10; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"lion", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom11; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"monkey", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom12; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"owl", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom13; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"pig", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom14; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"rooster", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom15; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sheep", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &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

And here's the .h file:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

@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
Community
  • 1
  • 1
Steee
  • 298
  • 2
  • 5
  • 13
  • Its a good question but the way you done it in post 1 is also perfectly fine. especially as its simply cut and paste after the initial code!! and then simple to drag and drop your buttons to the IB points! –  May 16 '13 at 11:12

2 Answers2

1

Use single AVAudioPlayer instance, and something like following..

-(void)playAudioOfType:(int)type{

     [self stopAudio];


     NSString *sound=@"";

     switch (type) {
       case 1:
          sound=@"bird";
        break;

       case 2:
          sound=@"bird2";
        break;

       case 3:
          sound=@"bird3";
        break;

       case 4:
          sound=@"chicken";
        break;

       default:
         break;
      }


      NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:sound
                                     ofType:@"mp3"]];



NSError *error;
if(url){
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:url
                   error:&error];



    if (error)
    {
        NSLog(@"Error in audioPlayer: %@", 
              [error localizedDescription]);
    } else {
        audioPlayer.delegate = self;
        [audioPlayer prepareToPlay];
    }

    [audioPlayer play];
}


 }

 -(void)stopAudio{

if(audioPlayer && [audioPlayer isPlaying]){
    [audioPlayer stop];
    audioPlayer=nil;
}


}

Then each of your action you can call the function like below

-(IBAction) boom; {
     [self playAudioOfType:1]; //Will play bird.mp3
}

-(IBAction) boom1; {
     [self playAudioOfType:2]; //Will play bird2.mp3
}

-(IBAction) boom2; {
     [self playAudioOfType:3]; //Will play bird3.mp3
}

Find a test app here .. You just need to add the mp3 files in the app and it should work..

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • so, do I need to erase all the -(IBAction) boom1; lines and replace it with that first bit of code? Sorry.. very new to objc and ios :| – Steee Mar 29 '13 at 05:28
  • No you don't have to remove the actions, you can use the same function in every actions with different type and define the sound file inside the function.. But better approach is to create a single action with different tag assigned to the button.. Tell me if you don't understand.. – iphonic Mar 29 '13 at 05:31
  • So you're saying take off the bits starting with "CFBundleRef..." and just put that second piece of code you included and then put the first bit of code before all that? if not.. then.. I don't understand :D – Steee Mar 29 '13 at 05:35
  • I'm so sorry.. I still don't understand. Should I erase my .h and .m files and include what you wrote? :/ I know it must be simple, I'm just very new. – Steee Mar 29 '13 at 05:49
  • Added a test app, see my comment. – iphonic Mar 29 '13 at 06:36
  • I'm getting some errors in the .m as well as the .h file mostly about the "unknown type name 'AVAudioPlayer'. I've imported all the frameworks into the project and into the header file. Here's a screenshot: http://s24.postimg.org/c0dru5wbp/Screen_Shot_2013_03_29_at_8_51_18_AM.png – Steee Mar 29 '13 at 13:52
  • bah! Sorry.. forgot to add the #import in the header. Thanks! Works perfectly! – Steee Mar 30 '13 at 01:58
0

try this link: Simple sound question

see in last of this question...

    -(void)stop
    {
            AudioServicesDisposeSystemSoundID (_soundID);
            self.IsPlaying = FALSE;
    }
DharaParekh
  • 1,730
  • 1
  • 10
  • 17