我已在记忆游戏应用程序中点击按钮设置音乐文件。
音乐已正常播放一段时间,但在点击按钮数量后,会失去应用中的声音效果。
-(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];
}
这对于按下的数量非常有效,但在点击后我无法播放此按钮点击音。
可能是什么问题&amp;如何解决这个问题。
有没有其他方式播放音频文件?
请建议适当的解决方案。
感谢。
答案 0 :(得分: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];
}
答案 1 :(得分: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
}
}
}