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

オーディオが停止したときにメモリを解放する方法はありますか?助けてください

4

1 に答える 1

0

AVAudioPlayer インスタンスをリークしているようです。デリゲートはオブジェクトへのポインターを保持しますが、通常、デリゲートは保持された参照を保持しません。したがって、あなたのコードはオブジェクトを割り当ててから、それらをリークしているようです。オブジェクトを保持するには、コードでプロパティ宣言を使用する必要があり、非アトミックと保持を使用します。

于 2013-07-15T22:04:41.900 に答える