1

我已经在记忆游戏应用程序中单击按钮设置了音乐文件。

音乐正常播放了一段时间,但在点击按钮的数量后,应用程序会失去声音效果。

-(void)setButtons_AsPerTheMatrixSelection
{
    for ( i = 0 ; i < cv ; ++i )
    {
        for ( j = 0 ; j < ch ; ++j )
        {
            btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(10+pw*j, 51+ph*i, width, height)] autorelease];
            btnMatrix.tag = i*ch+j;
            btnMatrix.userInteractionEnabled = TRUE;
            bulImageStatus = FALSE;
            [btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
            [btnMatrix setImage:imgDefaultBG forState:UIControlStateNormal];
            [viewWithButtons addSubview:btnMatrix];

            [arrButton addObject:btnMatrix];
        }
    }
}    

-(void)changeImage:(id)sender
{
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"mp3"];
    if (musicPath) 
        TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];

    [TapSoud setDelegate:self];
    [TapSoud prepareToPlay];
    [TapSoud play];
}

这对于按下次数非常有效,但是在某些单击后我无法播放此按钮单击音。

可能是什么问题以及如何解决这个问题。

有没有其他方法可以播放音频文件?

请提出适当的解决方案。

谢谢。

4

2 回答 2

1

AVAudioPlayer 被一遍又一遍地分配,但从未释放。

尝试这个。

-(void)changeImage:(id)sender
{
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"mp3"];
    if (musicPath) {
        if(TapSoud){ // TapSound ? 
             [TapSoud release];
              TapSoud = nil;
        }
        TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL 
fileURLWithPath:musicPath] error:nil];
}

[TapSoud setDelegate:self];
[TapSoud prepareToPlay];
[TapSoud play];

}

于 2012-06-04T07:59:44.153 回答
0

每次播放时使用相同的 AVAudioPlayer 参考:

-(void)changeImage:(id)sender
{
  NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"mp3"];
  if (musicPath) {
    if(!TapSoud){ // check TapSound has reference
         TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];
         [TapSoud setDelegate:self]
    }
    else
    {
        NSNumber *time = [NSNumber numberWithDouble:0];
        //NSTimeInterval interval = [time doubleValue];
       [TapSoud playAtTime:[time doubleValue];// Smae Sound played at time 0:00
    }

  }
 }
于 2012-06-04T09:07:54.670 回答