2013-06-06 57 views
0

在我的应用程序中,我使用了音频播放器,因为当我重复点击按钮时,内存随着它的增加而增加。在这里我的代码。内存不释放音频播放器

- (IBAction)didTapFlag:(id)sender 
{ 
int btnTag = ((UIButton *)sender).tag; 
NSLog(@"btnTag %d",btnTag); 
checkBtnCondition=YES; 
menuView.hidden=YES; 

if([audioPlayer isPlaying]) 
{ 
    [self playPauseAudio]; 
} 

NSLog(@"Check %c",iscommentOn); 
NSLog(@"swipecount%d",swipeCount); 

if(iscommentOn==NO) 
{if (btnTag == 0) 
{ 
    [mCommentView hide]; 
    // [mCommentView setHidden:YES]; 
    iscommentOn=YES; 
    flagBtn.tag=1; 
} 
else 
{ 
    [mCommentView setHidden:NO]; 
    [commentPlayer1 stop]; 
    iscommentOn=NO; 
    commentPlayer1 = nil; 
    flagBtn.tag = 0; 
} 
} 
else 
{ 
    iscommentOn=NO; 


    if(![commentPlayer1 isPlaying]) 
    { 
     iscommentOn=NO; 
    } 
    NSLog(@"->%c",iscommentOn); 
    if(!iscommentOn) 
    { 
     iscommentOn=YES; 
     if(mCommentView) 
     { 
      if (btnTag == 0) 
      { 
       [commentPlayer1 stop]; 
       commentPlayer1 = nil; 
       [mCommentView hide]; 
       flagBtn.tag = 1; 
      } 
      return; 
     } 
     float comx; 
     float comy; 
     if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) 
     { 
      comx = 200; 
      comy = 280; 
     } 
     else 
     { 
      comx = 50; 
      comy = 170; 
     } 
     mCommentView = [[CommentUtility alloc] initWithFrame:CGRectMake(comx, comy, 330, 250)]; 
     [mCommentView setDelegate:(id)self]; 
     [self.view addSubview:mCommentView]; 


     switch (swipeCount) { 
      case 1: 
       showComment = 0; 
       break; 
      case 5: 
       showComment = 1; 
       break; 
      case 11: 
       showComment = 2; 
       break; 
      case 15: 
       showComment = 3; 
       break; 
      case 19: 
       showComment = 4; 
       break; 
      case 22: 
       showComment = 5; 
       break; 

      default: 
       break; 
     } 

     if (language==1) { 
      NSLog(@"English"); 

      [mCommentView setCommentText:[commenttextEngArray objectAtIndex:showComment]]; 

     } else { 
      NSLog(@"Hindi"); 

      [mCommentView setCommentText:[commenttextHinArray objectAtIndex:showComment]]; 

     } 

     if (btnTag == 1) 
     { 
      [mCommentView show]; 
      iscommentOn=NO; 
      flagBtn.tag = 0; 
     } 

     if(commentPlayer1) 
      commentPlayer1 = nil; 

     NSURL *url; 
     if(swipeCount==1) 
     { 
      if (language==1) { 
       NSLog(@"English"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"kittur rani english" ofType:@"mp3"]]; 
      } else { 
       NSLog(@"Hindi"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"raniChinamma" ofType:@"mp3"]]; 
      } 
     } 
     if(swipeCount==5) 
     { 
      if (language==1) { 
       NSLog(@"English"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"AvantiBai English" ofType:@"mp3"]]; 
      } else { 
       NSLog(@"Hindi"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"RaniAvantiBai" ofType:@"mp3"]]; 
      } 
     } 
     if(swipeCount==11) 
     { 
      if (language==1) { 
       NSLog(@"English"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Bikaji Cama English" ofType:@"mp3"]]; 
      } else { 
       NSLog(@"Hindi"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"BhikajiCama" ofType:@"mp3"]]; 
      } 
     } 
     if(swipeCount==15) 
     { 
      if (language==1) { 
       NSLog(@"English"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Begum of Avadh english" ofType:@"mp3"]]; 
      } else { 
       NSLog(@"Hindi"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Begumhazrat" ofType:@"mp3"]]; 
      } 
     } 
     if(swipeCount==22) 
     { 
      if (language==1) { 
       NSLog(@"English"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"kasturba gandhi" ofType:@"mp3"]]; 
      } else { 
       NSLog(@"Hindi"); 
       url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"KasturbaGandhi" ofType:@"mp3"]]; 
      } 
     } 

     NSLog(@"url 3 %@", url); 

     NSError *error; 
     commentPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
     [commentPlayer1 setDelegate:(id)self]; 
     if(readMode != 2) 
     { 
      [commentPlayer1 setVolume:5.0f]; 
     } 
     else{ 
      [commentPlayer1 setVolume:0.0f]; 
     } 
     [commentPlayer1 prepareToPlay]; 
     [commentPlayer1 play]; 

    } 
} 

} 

我加入的代码中找到的内存使用量

-(void) logMemUsage { 
// compute memory usage and log if different by >= 100k 
int cacheSizeMemory = 16*1024*1024; // 16MB 
int cacheSizeDisk = 32*1024*1024; // 32MB 
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"]; 
[NSURLCache setSharedURLCache:sharedCache]; 


static long prevMemUsage = 0; 
long curMemUsage = usedMemory(); 
long memUsageDiff = curMemUsage - prevMemUsage; 

if (memUsageDiff > 100000 || memUsageDiff < -100000) { 
    prevMemUsage = curMemUsage; 
    NSLog(@"Memory used %7.1f (%+5.0f), free %7.1f kb", curMemUsage/1000.0f, memUsageDiff/1000.0f, freeMemory()/1000.0f); 
} 
} 

,因为它显示日志作为

2013-06-06 13:44:27.287 Jansirani[8716:707] Memory used 23396.4 (+1794), free 28647.4 kb 
    2013-06-06 13:44:37.286 Jansirani[8716:707] Memory used 26214.4 (+2818), free 32739.3 kb 
    2013-06-06 13:45:37.284 Jansirani[8716:707] Memory used 29466.6 (+1855), free 35049.5 kb 

有什么方法释放内存时,音频停止?请帮我

+0

您可以检查这些链接.. http://stackoverflow.com/questions/1352588/avaudioplayer-memory-leak http://stackoverflow.com/questions/7221024/avaudioplayer-leaks http://stackoverflow.com/questions/5932913/massive-avaudioplayer-memory-leaks – Venkat

+0

用仪器内存泄漏来剖析你的应用程序。它会指向您正在泄漏内存的确切线路。另外,当问一个关于内存泄漏的问题时,请不要忘记提及是否使用ARC。 – Mar0ux

回答

0

看起来你只是漏了AVAudioPlayer实例。一个委托持有一个指向一个对象的指针,但通常委托不包含保留的引用。所以,你的代码似乎分配对象,然后泄漏它们。为了坚持一个对象,代码必须使用属性声明并使用非原子并保留。