在我的应用程序中,我使用了音频播放器,因为当我反复单击按钮时,内存会随着时间的推移而增加。这是我的代码。
- (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
音频停止时有什么办法释放内存吗?请帮帮我