title: how to display loading indicator in AVAudioPlayer if user clicks the play button
body: <p>I am working in AVAudioplayer in iOS. I am displaying an activity indicator for loading time if a user clicks the play button. My problem is that when I click the play button the loading time activity indicator is not displayed.</p>
  
  <pre><code>-(void)loadData
  
  {
  
  url=[NSURL URLWithString:@"http://www.urls.com"];
  
  NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];
  
  [[NSURLConnection alloc]initWithRequest:request delegate:self];
  
  [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
  
  [self.view addSubview:loadingView];
  
  [activityView startAnimating];
  
  loadingConnection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
  
  }
  
  -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  
  {
  
  responseData=[[NSMutableData alloc]init];
  
  [responseData setLength:0];
  
  }
  
  -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  
  {
  
  if(connection==loadingConnection)
  
  {
  
  [responseData appendData:data];
  
  }
  
  }
  
  -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  
  {
  
  [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
  
  [activityView stopAnimating];
  
  [loadingView removeFromSuperview];
  
  if(connection==loadingConnection)
  
  {
  
  NSError *jsonError = nil;
  
  id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&amp;jsonError];
  
  if ([jsonObject isKindOfClass:[NSArray class]])
  
  {
  
  //NSArray *jsonArray = (NSArray *)jsonObject;
  
  }
  
  else if ([jsonObject isKindOfClass:[NSDictionary class]])
  
  {
  
  NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
  
  NSArray *array=[[NSArray alloc]init];
  
  array=[jsonDictionary objectForKey:@"audio-urls"];
  
  dataDictionary=[array objectAtIndex:0];
  
  NSLog(@"%@",dataDictionary);
  
  }
  
  [urlsArray addObject:[dataDictionary objectForKey:@“Audio1”]];
  
  [urlsArray addObject:[dataDictionary objectForKey:@“Audio2”]];
  
  [urlsArray addObject:[dataDictionary objectForKey:@“Audio3”]];
  
  [urlsArray addObject:[dataDictionary objectForKey:@“Audio4”]];
  
  [urlsArray addObject:[dataDictionary objectForKey:@“Audio5”]];
  
  [urlsArray addObject:[dataDictionary objectForKey:@“Audio6”]];
  
  NSLog(@"%@",urlsArray);
  
  }
  
  }
  
  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  
  {
  
  [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
  
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Please ensure you have internet connection" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  
  [alert show];
  
  [activityView stopAnimating];
  
  [loadingView removeFromSuperview];
  
  }
  
  - (void)viewDidLoad
  
  {
  
  playing=NO;
  
  [self loadData];
  
  //  [self temp];
  
  [self performSelector:@selector(loadData) withObject:nil afterDelay:3.0f];
  
  urlsArray=[[NSMutableArray alloc]init];
  
  [super viewDidLoad];
  
  }    
  
  -(void)temp
  
  {
  
  // loading View
  
  loadingView=[[UILabel alloc]initWithFrame:CGRectMake(135, 200, 40, 40)];
  
  loadingView.backgroundColor=[UIColor clearColor];
  
  loadingView.clipsToBounds=YES;
  
  loadingView.layer.cornerRadius=10.0;
  
  activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  
  activityView.frame = CGRectMake(10, 11, activityView.bounds.size.width, activityView.bounds.size.height);
  
  [loadingView addSubview:activityView];
  
  }
  
  
  -(void)playOrPauseButtonPressed:(id)sender
  
  {
  
  if(playing==NO)
  
  {
  
  [self temp];
  
  // [self.view addSubview:loadingView];
  
  //   [loadingView addSubview:activityView];
  
  // [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
  
  [playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];
  
  // Here Pause.png is a image showing Pause Button.
  
  NSError *err=nil;
  
  AVAudioSession *audioSession=[AVAudioSession sharedInstance];
  
  [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
  
  NSLog(@"%@ %d",urlsArray,selectedIndex);
  
  NSString *sourcePath=[urlsArray objectAtIndex:selectedIndex];
  
  NSData *objectData=[NSData dataWithContentsOfURL:[NSURL URLWithString:sourcePath]];
  
  NSLog(@"%@",objectData);
  
  audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&amp;err];
  
  if(err)
  
  {
  
  NSLog(@"Error %ld,%@",(long)err.code,err.localizedDescription);
  
  }
  
  NSTimeInterval bufferDuration=0.005;
  
  [audioSession setPreferredIOBufferDuration:bufferDuration error:&amp;err];
  
  if(err)
  
  {
  
  NSLog(@"Error %ld, %@", (long)err.code, err.localizedDescription);
  
  }
  
  double sampleRate = 44100.0;
  
  [audioSession setPreferredSampleRate:sampleRate error:&amp;err];
  
  if(err)
  
  {
  
  NSLog(@"Error %ld, %@",(long)err.code,err.localizedDescription);
  
  }
  
  [audioSession setActive:YES error:&amp;err];
  
  if(err)
  
  {
  
  NSLog(@"Error %ld,%@", (long)err.code, err.localizedDescription);
  
  }
  
  sampRate=audioSession.sampleRate;
  
  bufferDuration=audioSession.IOBufferDuration;
  
  NSLog(@"SampeRate:%0.0fHZI/OBufferDuration:%f",sampleRate,bufferDuration);
  
  audioPlayer.numberOfLoops = 0;
  
  [audioPlayer prepareToPlay];
  
  [audioPlayer play];
  
  audioPlayer.delegate=self;
  
  if(!audioPlayer.playing)
  
  {
  
  [audioPlayer play];
  
  }
  
  playing=YES;
  
  }
  
  else if (playing==YES)
  
  {
  
  [playButton setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
  
  [audioPlayer pause];
  
  playing=NO;
  
  timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateViewForPlayerState) userInfo:nil repeats:YES];
  
  }
  
  if (self.audioPlayer)
  
  {
  
  [self updateViewForPlayerInfo];
  
  [self updateViewForPlayerState];
  
  [self.audioPlayer setDelegate:self];
  
  }
  
  }
  </code></pre>