how to record voice and play in iphone
<p>I have implemented voice recording functionality in our project but i couldn't have control on stop and discard buttons. In second time not able to record the voice after clicking the discard button.
After clicking discard also the audio file is playing and not able to click start button again.</p>

<p>Please help on this one</p>

<p>Here is the source code</p>

<pre><code>.h file

@interface VoiceInput : UIViewController
                                 &lt;AVAudioRecorderDelegate&gt;
{
//Audio record

  float remainingDelayTime;
  float remainingRecordTime;
  UILabel *delayLabel;

  UIProgressView *progressView;
  AVAudioRecorder *recorder;
  NSTimer *delayTimer;
  NSTimer *recordTimer;
  BOOL toggle;
  NSURL *recordedTmpFile;
  NSError *error;
}
@property (nonatomic, retain) AVAudioRecorder *recorder;
@property (nonatomic, retain) NSTimer *delayTimer;
@property (nonatomic, retain) NSTimer *recordTimer;
@end

.m file

@implementation VoiceInput
@synthesize progressView;
@synthesize recorder;
@synthesize delayTimer;
@synthesize recordTimer;

- (void)viewDidLoad
{
    toggle = YES;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&amp;error];
    [audioSession setActive:YES error:&amp;error];
}

-(void)startPushed
{
   if (toggle)
    {
        remainingDelayTime = 4.0;
        delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                      target:self
                                                    selector:@selector(delayTimerFired:)
                                                    userInfo:nil
                                                     repeats:YES];
        toggle = NO;
        NSMutableDictionary *rs = [[NSMutableDictionary alloc] init];
        [rs setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
        [rs setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [rs setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
        NSLog(@"USING FILE CALLED: %@", recordedTmpFile);

        recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:rs error:&amp;error];
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];

    }
    else
    {
        toggle = YES;
        NSLog(@"Using File Called: %@", recordedTmpFile);
        [recorder stop];
    }


  self.discardButton.enabled = NO;
  self.startButton.enabled = NO;
  self.stopButton.enabled = YES;

}

-(void)stopPushed
{
if([self.recorder isRecording])
  {
    [self.recorder stop];

    if(remainingRecordTime &gt;= 1.0)
    {
      [self.recordTimer invalidate];
    }
}
self.delayLabel.textColor = [UIColor darkGrayColor];
 self.delayLabel.text = [[NSString alloc] initWithFormat:@"Record in ..."];

  self.discardButton.enabled = YES;
  self.playbackButton.enabled = YES;
  self.startButton.enabled = NO;
  self.stopButton.enabled = NO;

}

-(void)playbackPushed
{ 
  AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile    error:&amp;error];
    [avPlayer prepareToPlay];
    [avPlayer play];

  self.discardButton.enabled = YES;
  self.playbackButton.enabled = YES;
  self.returnButton.enabled = YES;
  self.startButton.enabled = YES;
  self.stopButton.enabled = YES;

}

-(void)discardPushed
{
  [self.recorder deleteRecording];
  self.progressView.progress = 0;
  self.delayLabel.textColor = [UIColor darkGrayColor];
  self.delayLabel.text = @"Record in ...";

  self.discardButton.enabled = NO;
  self.playbackButton.enabled = NO;
  self.startButton.enabled = YES;
  self.stopButton.enabled = YES;

}

-(void)delayTimerFired:(NSTimer *)theDelayTimer
{
  self.progressView.progress = 0;
  remainingDelayTime -= 1.0;
  NSLog(@"fired %f", remainingDelayTime);
  self.delayLabel.textColor = [UIColor blackColor];
  self.delayLabel.text = [[NSString alloc] initWithFormat:@"Record in %2.0f",
                            remainingDelayTime];

  if(remainingDelayTime &lt;= 0.0)
  {
    [self.delayTimer invalidate];
    self.delayLabel.text = [[NSString alloc] initWithFormat:@"Recording"];

    [self.recorder recordForDuration:TIME];
    remainingRecordTime = TIME;
    recordTimer = [NSTimer scheduledTimerWithTimeInterval:TIME_DECREMENT
                                             target:self
                                           selector:@selector(recordTimerFired:)
                                           userInfo:nil
                                            repeats:YES];
  }

}

-(void)recordTimerFired:(NSTimer *)theRecordTimer
{
  remainingRecordTime -= TIME_DECREMENT;
  NSLog(@"fired %f", remainingRecordTime);
  self.progressView.progress = (TIME - remainingRecordTime)/TIME;

  if(remainingRecordTime &lt;= 0.0)
  {
    [self.recordTimer invalidate];
  }

}
</code></pre>

<p>Thanks in advance</p>
