2013-06-06 196 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實例。一個委託持有一個指向一個對象的指針,但通常委託不包含保留的引用。所以,你的代碼似乎分配對象,然後泄漏它們。爲了堅持一個對象,代碼必須使用屬性聲明並使用非原子並保留。