1

Sir, What do you think the error of my Code.. because i cant record a Audio. can you help me in my project? i want to make a simple Recording Project. with three Buttons (PLAY, STOP, RECORD)...by the way i didnt use the nib file. im newbie in Objective-C my approach is purely Programmatically..Thanks in advance more power..

and this is my code in viewDidLoad()

-(void)viewDidLoad
{
    [super viewDidLoad];{
        playButton.enabled = NO;
    stopButton.enabled = NO;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16], 
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2], 
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0], 
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);

    }
    else 
    {
        [audioRecorder prepareToRecord];
    }

}


-(void) recordButton:(UIButton *)sender
{
        if (!audioRecorder.recording)
        {

            playButton.enabled = NO;
            stopButton.enabled = YES;
            [audioRecorder record];
             NSLog(@"Record");
        }
}


-(void)stop:(UIButton *)sender
{
        stopButton.enabled = NO;
        playButton.enabled = YES;
        recordButton.enabled = YES;

        if (audioRecorder.recording)
        {
            [audioRecorder stop];
            NSLog(@"Stop");
        } 
        else if (audioPlayer.playing) 
        {
            [audioPlayer stop];
        }
}

-(void) playAudio:(UIButton *)sender
{
    NSError *error;
        if (!audioRecorder.recording)
        {
            stopButton.enabled = YES;
            recordButton.enabled = NO;
             NSLog(@"Play");
            if (audioPlayer)
            {


            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error];

            audioPlayer.delegate = self;
            }
            if (error)

            { NSLog(@"Error: %@", 
                      [error localizedDescription]);
            }
            else
                [audioPlayer play];
        }
}

2 Answers2

0

Apple provides a sample application called SpeakHere that would greatly help you in using Audio File Services to create, record into, and read from a CAF (Core Audio Format) audio file.

You can find it on Apple's Developer site here.

Hope this helps.

werner
  • 859
  • 4
  • 8
  • sir thanks for that info.. but i downloaded that..and scan, but still im having trouble on it – Gerald Stojakovic Jul 16 '12 at 13:11
  • This [tutorial](http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1) may be more interesting then.There is a zip with the complete solution at the end. – werner Jul 16 '12 at 13:15
  • thanks again for that link sir.. do you have tutorial on the same issue but not in .xib/(IBOutlet or IBAction) approach? thanks again – Gerald Stojakovic Jul 16 '12 at 13:17
  • Why don't you want to use XIB files and Interface Builder ? It is a lot easier than writing the code to display the buttons and wire them up to the actions, but it is not impossible. [This](http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically) explains how to display a `UIButton` programmatically. – werner Jul 16 '12 at 13:26
0

First of all, move your code out from the viewDidLoad to either viewDidAppear or to a function call. Next, read about AVAudioSession. Briefly, you want to change its category to either AVAudioSessionCategoryRecord or AVAudioSessionCategoryPlay, when you will record or play respectively.

- (void)beginRecording {

  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  NSError *err = nil;
  [audioSession setCategory:AVAudioSessionCategoryRecord error:&err];
  if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
  }
  err = nil;
  [audioSession setActive:YES error:&err];
  if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
  }
  if (audioSession.inputIsAvailable) {
    if ([audioRecorder prepareToRecord]) {
      [audioRecorder record];
    }
    else {
      UIAlertView *alert =
      [[UIAlertView alloc] initWithTitle:@"Error!"
                                 message:@"Could not begin recording"
                                delegate:nil
                       cancelButtonTitle:@"OK"
                       otherButtonTitles:nil];
      [alert show];
      [alert release];
    }
  }
}

- (void)stopRecording {
  [audioRecorder stop];
}

These are the minimum parameters you need to begin recording (at the very least they worked for me, but you can set the quality to any value you want as AppleLoseless weights a ton, but note that min quality is the shi**iest thing in the known galaxy):

  NSMutableDictionary *settings = [[[NSMutableDictionary alloc] init] autorelease];
  [settings setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
  [settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
  [settings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
  [settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
  [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
  [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSError *err = nil;
    self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:url
                                                        settings:settings
                                                           error:&err];
    if(err){
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle:@"Warning"
                                   message:[err localizedDescription]
                                  delegate:nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        [alert release];
      }

Please note that I completely disregard the memory management, this post is not a memory management guide.

Eugene
  • 10,006
  • 4
  • 37
  • 55