Big Box Developer
Big Box Developer

Reputation: 39

How to record sound in iOS?

I am currently wondering how you can record audio in iOS. I know many people comprehend this as recording from a microphone and playing it back, but this is not the case. I am making a sound recording application for the iPad. In the GarageBand application that Apple has on the iOS App store, you can record your own sounds and play them back from within the application. If this doesn't make sense, think of it as this:

All I am trying to do is make a button that plays a sound. I need to know how to record that button sound and be able to play the sound sequence back. So if I pressed "record" then buttons "A, F, J" and then "stop" then press "play" it would playback what it recorded (sounds A F and J).

I am trying to make it so that you can record and make your own music within this app. Sorry if this is confusing, please help me to the best of your abilities. Thanks!

Upvotes: 3

Views: 1624

Answers (1)

Theme
Theme

Reputation: 417

You could create two NSMutableArrays and empty them when you hit record. You would also need an NSTimer and an int. So in header:

NSTimer *recordTimer;
NSTimer *playTimer;
int incrementation;
NSMutableArray *timeHit;
NSMutableArray *noteHit;

Include in your header all of the voids and IBActions and such below.

Make your sound buttons all have different, unique tags.

and then in your main file:

-(void)viewDidLoad {

    timeHit = [[NSMutableArray alloc] init];
    noteHit = [[NSMutableArray alloc] init];

}

-(IBAction)record {

    recordTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(timerSelector) userInfo:nil repeats:YES];
    [timeHit removeAllObjects];
    [noteHit removeAllObjects];
    incrementation = 0;
}

-(void)timerSelector {

    incrementation += 1;

}

-(IBAction)hitSoundButton:(id)sender {

    int note = [sender tag];
    int time = incrementation;

    [timeHit addObject:[NSNumber numberWithInt:time]];
    [noteHit addObject:[NSNumber numberWithInt:note]];
    [self playNote:note];
}

-(IBAction)stop {

    if ([recordTimer isRunning]) {

        [recordTimer invalidate];
    } else if ([playTimer isRunning]) {

        [playTimer invalidate];
    }

}

-(IBAction)playSounds {

    playTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(playback) userInfo:nil repeats:YES];

    incrementation = 0;



}


-(void)playback {

    incrementation += 1;

    if ([timeHit containsObject:[NSNumber numberWithInt:incrementation]]) {

        int index = [timeHit indexOfObject:[NSNumber numberWithInt:incrementation]];

        int note = [[noteHit objectAtIndex:index] intValue];

        [self playNote:note];
    }
}


-(void)playNote:(int)note {


    //These notes would correspond to the tags of the buttons they are played by.

    if (note == 1) {
        //Play your first note
    } else if (note == 2) {
        //Play second note
    } else if (note == 3) {
       //And so on
    } else if (note == 4) {
            //etc.
    }

}

With a little bit of fiddling (I doubt this code is perfect), you might get this to work. Like you will probably want the play/record buttons to be disabled once you hit one of them. Good luck!

Upvotes: 1

Related Questions