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 に答える