Ethan Parker joining your group, I'm a new iOS dev

10 views
Skip to first unread message

Ethan

unread,
Feb 6, 2012, 4:03:31 PM2/6/12
to BYU CocoaHeads
Hey Guys. I'm excited to have joined your group! I've been developing
a planner application to going along with my business, Parker Planners
for a while now.

What I've been working on recently is sound boards for entertainment
purposes.

Anyone know how to make a button cycle through 3 or 4 sounds? As in
push button 1- play sound 1 push button 1 -play sound 2 push button 1 -
play sound 3 push button1 - play sound 1. Sort of like that.

Thanks!

Ethan

Robert Brown

unread,
Feb 6, 2012, 4:08:52 PM2/6/12
to byu-coc...@googlegroups.com
I would have an array of sound files and a counter. When the button is pressed, play the sound at the current counter index. Then increment the counter and modulus the counter by the size of the array. The modulus operation (remainder division) guarantees that the counter is always within the range of the array.

--
Robert Brown
iOS Developer

> --
> This was sent to members of the "BYU CocoaHeads" google group.
> To post to this group, send email to byu-coc...@googlegroups.com
> For our job posting guidelines see http://cocoaheads.byu.edu/wiki/cocoaheads-job-posting-guidelines
> To unsubscribe from this group, send email to
> byu-cocoahead...@googlegroups.com
> Be sure to visit our website at http://cocoaheads.byu.edu

Ethan Parker

unread,
Feb 6, 2012, 4:16:06 PM2/6/12
to byu-coc...@googlegroups.com
Wow, thanks for the quick reply, Robert. I'm a total noob though, right now I just have the sound files in "supporting files" in the navigator on the left hand side of Xcode. Would the 'array' be a new file like a .h or a .m? Is the counter a function declared in the .h or .m or appdelegate.m or a whole new file? Just not sure where to start.

I already have a soundboard in the app store and it's doing pretty well but I just have it playing one sound at a time real simple-like.

Sean Hess

unread,
Feb 6, 2012, 4:36:37 PM2/6/12
to byu-coc...@googlegroups.com
Hi Ethan, 

Do you have a View Controller that controls the sound that plays when you click a button? Or did you wire everything up in interface builder or something?

On Feb 6, 2012, at 2:16 PM, Ethan Parker wrote:

Wow, thanks for the quick reply, Robert. I'm a total noob though, right now I just have the sound files in "supporting files" in the navigator on the left hand side of Xcode. Would the 'array' be a new file like a .h or a .m? Is the counter a function declared in the .h or .m or appdelegate.m or a whole new file? Just not sure where to start.

I already have a soundboard in the app store and it's doing pretty well but I just have it playing one sound at a time real simple-like.

Robert Brown

unread,
Feb 6, 2012, 4:43:44 PM2/6/12
to byu-coc...@googlegroups.com
It would look something like this. I'm assuming you are using an IBAction. If not, just replace IBAction with void and remove the sender. I also don't know how you are handling sounds, so I just used the simplest sound playing possible. There is a lot of C code in here though. Hopefully it isn't confusing. 

#import <AudioToolbox/AudioToolbox.h>

#import "SoundPlayer.h"


@interface SoundPlayer ()

@property (nonatomic, copy) NSArray * sounds;
@property (nonatomic, assign) NSUInteger soundIndex;

@end


@implementation SoundPlayer

@synthesize sounds     = _sounds;
@synthesize soundIndex = _soundIndex;

- (id)init {

    

    if ((self = [super init])) {

        

        // Grabs the URLs to all the sounds.
        NSURL * sound1URL = [[NSBundle mainBundle] URLForResource:@"sound1.mp3"
                                                    withExtension:nil];
        NSURL * sound2URL = [[NSBundle mainBundle] URLForResource:@"sound2.mp3"
                                                    withExtension:nil];
        NSURL * sound3URL = [[NSBundle mainBundle] URLForResource:@"sound3.mp3"
                                                    withExtension:nil];

        

        // !!!: ou may want to check that these URLs are valid.

        

        SystemSoundID soundID1, soundID2, soundID3;

        

        // Creates the sounds as system sounds.
        // !!!: I'm not sure if the cast is right.
        AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef) sound1URL, &soundID1);
        AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef) sound2URL, &soundID2);
        AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef) sound3URL, &soundID3);

        

        // Stores the sounds in an array.
        [self setSounds:[NSArray arrayWithObjects:
                         [NSValue valueWithPointer:&soundID1],
                         [NSValue valueWithPointer:&soundID2],
                         [NSValue valueWithPointer:&soundID3],
                         nil]];

        

        // Gives the sound index a default value.
        [self setSoundIndex:0];
    }

    

    return self;
}

- (void)dealloc {

    

    // Disposes all the sounds.
    for (NSValue * value in [self sounds]) {

        

        SystemSoundID soundID;
        [value getValue:&soundID];

        

        AudioServicesDisposeSystemSoundID(soundID);
    }
}

- (IBAction)playSound:(id)sender {

    

    // Gets the current sound.
    SystemSoundID soundID;
    NSValue * value = [[self sounds] objectAtIndex:[self soundIndex]];
    [value getValue:&soundID];

    

    // Plays the sound.
    AudioServicesPlaySystemSound(soundID);

    

    // Increments the counter and clamps it to the array range.
    NSUInteger index = [self soundIndex];
    index = ++index % [[self sounds] count];
    [self setSoundIndex:index];
}

@end

--
Robert Brown
iOS Developer




On Feb 6, 2012, at 2:16 PM, Ethan Parker wrote:

Wow, thanks for the quick reply, Robert. I'm a total noob though, right now I just have the sound files in "supporting files" in the navigator on the left hand side of Xcode. Would the 'array' be a new file like a .h or a .m? Is the counter a function declared in the .h or .m or appdelegate.m or a whole new file? Just not sure where to start.

I already have a soundboard in the app store and it's doing pretty well but I just have it playing one sound at a time real simple-like.

Ethan Parker

unread,
Feb 6, 2012, 5:33:19 PM2/6/12
to byu-coc...@googlegroups.com
Sean and Robert- I have a button in the .xib file that is linked to an IBAction in the .m file. I'm using the AVFoundation.framework file that I added to the project using Build Phases > link binary with libraries. I put this at the top of the header file: #import <AVFoundation/AVFoundation.h>

Then in the .m file I have a function and then instances of the function like this:

- (void) playSound:(NSString*) filename {
   
    NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat: filename, [[NSBundle mainBundle] resourcePath]]];
   
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &error];
    audioPlayer.numberOfLoops = 0;
   
    [audioPlayer play];
   
}

- (IBAction)audio1:(id)sender { [self playSound:@"%@/audiofile1.mp3"]; }

Robert- thanks for the help. Does C work in Xcode? How would this look using the AVFoundation file?

Robert Brown

unread,
Feb 6, 2012, 5:50:01 PM2/6/12
to byu-coc...@googlegroups.com
Objective-C and C can be mixed as needed. In your case you don't need any C code. 

To change this to AVPlayer, you can store the NSURLs in the array instead of SystemSoundIDs wrapped in NSValue objects. From there you should be able to figure out the necessary changes. 


--
Robert Brown
iOS Developer

Reply all
Reply to author
Forward
0 new messages