简体   繁体   English

内存未释放音频播放器

[英]Memory is not release audio player

In my application, i have used the audio player , Since when i click the button repeatedly , The memory is increased as and it goes. 在我的应用程序中,我使用了音频播放器,因为当我反复单击该按钮时,内存随其增加。 Here my code. 这是我的代码。

- (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];

    }
}

}

I have added the code find the memory usage 我添加了代码以查找内存使用情况

 -(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);
}
}

Since it shows the log as 由于它显示日志为

  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

Is there any way release memory when audio stops?Please help me out 音频停止时有什么释放记忆的方法吗?请帮帮我

It looks like you are just leaking the AVAudioPlayer instance. 看来您只是在泄漏AVAudioPlayer实例。 A delegate holds a pointer to an object, but in general a delegate does not hold a retained reference. 委托持有指向对象的指针,但是通常,委托不持有保留的引用。 So, your code seem to allocate objects and then just leak them. 因此,您的代码似乎分配对象,然后泄漏它们。 In order to hold on to an object, code must use a property declaration and uses nonatomic and retain. 为了保留对象,代码必须使用属性声明,并使用非原子和保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM