title: iPhone: Delegate method not being called for AVAudioPlayer and AVAudioRecorder
body: <p>The problem I am facing is with play. When I record that is storing in document directory with format of document even with size that means recording is being done. And when my <code>play</code> function is calling I am converting it to <code>NSData</code>, though it is not playing anything.</p>
  
  <pre><code>- (void)record{  
  
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      NSError *err = nil;
      [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&amp;err];
      if(err){
          NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
          return;
      }
      [audioSession setActive:YES error:&amp;err];
      err = nil;
      if(err){
          NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
          return;
      }
  
      if(recorder)
      {
          if(recorder.recording)
              [recorder stop];
          [recorder release];
          recorder = nil;
      }
  
      NSString *fileName = nil;
      if(![myItem audioURL]){
          fileName = [fileFormatter stringFromDate:[NSDate date]];
          [myItem setAudioURL:fileName];        
      } else{
          fileName = [myItem audioURL];}
  
      NSString *pathString = [NSString stringWithFormat:@"%@/%@", [delegate applicationDocumentsDirectory], fileName];
  
      NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                      [NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
                                      [NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey,
                                      [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                      [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil];
      recorder = [[AVAudioRecorder alloc] initWithURL: [NSURL fileURLWithPath:pathString] settings: recordSettings error: nil];
      recorder.delegate = self;
      if ([recorder prepareToRecord] == YES){
          [recorder record];       
          recording = YES;
  
      [pauseButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
      [pauseButton setEnabled:YES];
      [playButton setEnabled:NO];
      [recordButton setEnabled:NO];
  
  
      [self beginAnimation];
      //    NSError *error=nil;
      //    if(![context save: &amp;error])
      //    {
      //        //Couldn't save
      //    }
  }
  
  }
  
  - (void)play{
  
     /* [self silenced];
      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
  
      AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,          
                               sizeof (audioRouteOverride),&amp;audioRouteOverride);
  
      UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
      AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &amp;sessionCategory);    
      */
  
  
      if(pausedPlayback){
          [player play];
          playing = YES;
          pausedPlayback = NO;
          [pauseButton setEnabled:YES];
          [playButton setEnabled:NO];
          [self beginAnimation];
          return;
      }
      NSString *fileName = nil;
      if(![myItem audioURL]){
          return;
      } else
          fileName = [myItem audioURL];
      NSLog(@"file name :%@",fileName);
     // NSError *error;
  
      NSString *pathString = [NSString stringWithFormat:@"%@/%@", [delegate applicationDocumentsDirectory], fileName];
      NSLog(@"Path : %@",pathString);
  
  
  //  player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:pathString] error:nil] ;
  
  
  
      NSData *soundData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pathString]];
  
  
      player = [[AVAudioPlayer alloc] initWithData:soundData error: nil];         
      player.delegate = self;
      player.volume=1.0;  
  
      // Play the audio
      [player prepareToPlay];             
      [player play]; 
      playing = YES;  
  
      [recordButton setEnabled:NO];
      [pauseButton setEnabled:YES];
      [playButton setEnabled:NO];
  //    if (player==nil) {
  //        NSLog(@"%@",[error description]);
  //    }
  //    else
  //    {
        /*  player.volume=1.0;
          NSLog(@"about to play");
          [player prepareToPlay];
          [player play];
          NSLog(@"player play");
          [player setDelegate:self];
          playing = YES;  
  
          [recordButton setEnabled:NO];
          [pauseButton setEnabled:YES];
          [playButton setEnabled:NO];*/
  
  
          [self beginAnimation];
     // }
  }
  - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)_recorder successfully:(BOOL)flag{
      NSLog (@"audioRecorderDidFinishRecording:successfully:");
      [recorder release];
      recorder = nil;
  }
  
  - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)_player successfully:(BOOL)flag{
      if(![player isPlaying])
      {
          [player stop];
      }
      [player release];
      player = nil;
      playing = NO;
      [playButton setEnabled:YES];
      [pauseButton setEnabled:NO];
      [recordButton setEnabled:YES];
      [self stopAnimation];
  
  }
  </code></pre>