title: About auto record and auto play with AVAudioPlayer&AVAudioRecorder
body: <p>in .h file I wrote like this:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "SCListener.h"
  #import &lt;AVFoundation/AVFoundation.h&gt;
  
  @interface TalkingAndSayingViewController : UIViewController&lt;AVAudioPlayerDelegate&gt;
  {
  
  IBOutlet UIImageView *bkg;
  IBOutlet UILabel *showVoulme;
  
  SCListener *listener;
  
  NSTimer *time;
  
  BOOL listen;
  NSString *audioPath;
  AVAudioRecorder *recoder;
  AVAudioPlayer *player;
  
  int breakCount;
  
  BOOL timeActive;
  
  }
  @property(nonatomic,assign)int roleIndex;
  @end
  </code></pre>
  
  <p>and in the .m file I wrote this:</p>
  
  <pre><code>- (void)viewDidLoad
  {
  
  [super viewDidLoad];
  
  NSArray *searchPaths =  NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
  audioPath = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"audio.wav"];
  [audioPath retain];
  
  listen = YES;
  
  NSMutableDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                                         [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                                         [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                                         [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                                         nil];
  
  NSString *imgName = [NSString stringWithFormat:@"role%d.png",self.roleIndex];
  [bkg setImage:[UIImage imageNamed:imgName]];
  
  recoder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioPath] settings:recordSettings error:nil];
  [recoder setMeteringEnabled:YES];
  
  player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:nil];
  [player setNumberOfLoops:0];
  
  AVAudioSession *audioSession = [[AVAudioSession sharedInstance] retain];
  [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
  [audioSession setActive:YES error: nil];
  
  time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(CheckVolume) userInfo:nil repeats:YES];
  [[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];
  
  }
  
  -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
  {
       timeActive = YES;
          listen = YES;
          NSLog(@"fihish playing, start listen");
  }
  
  -(void)CheckVolume
  
      {    
          Float32 peak = [[SCListener sharedListener] peakPower];
          if(timeActive)
          {
              if(listen)
              {
                  [showVoulme setText:[NSString stringWithFormat:@"%f",peak]];
                  if(peak&gt;0.3)
                  {
                      listen = NO;
                      [recoder prepareToRecord];
                      [recoder record];
                      NSLog(@"start recording");
                  }
              }
  
              else
              {
                  if(peak&lt;0.1)
                  {
                      if(breakCount &gt;= 5)
                      {
                          NSLog(@"start play");
  
                          [recoder stop];
  
                          player.delegate = self;
                          [player prepareToPlay];
                          [player play];
                          timeActive = NO;
                          breakCount = 0;
                      }
                      else 
                          breakCount++;
                  }
                  else
                      breakCount = 0;
              }
          }
      }
  
  -(void)viewWillDisappear:(BOOL)animated
  {
      [recoder stop];
      [[SCListener sharedListener] stop];
  
      timeActive = NO;
  }
  
  -(void)viewDidAppear:(BOOL)animated
  {
      timeActive = YES;
  
      [[SCListener sharedListener] listen];
  
      breakCount = 0;
  }
  </code></pre>
  
  <p>Here have some problems,
  first:audioPlayerDidFinishPlaying  this function didn't do at all, and this code only can run to "start play", but the sound didn't play in fact, I used to use the recorder to check the volume when it's in the recording condition, but the peakPowerForChannel:0 only returns a static value of 0.00001, I have no idea to solve this problem, anyone can help me or send me a sample code of auto record and auto play? I really appreciate it. Thanks </p>