0

我在表格视图单元格上放了一个按钮,当按下按钮时它会播放声音,当我按下按钮时它会停止声音。但是按下另一个单元格上的另一个按钮,它不会播放另一个声音,而另一个仍在播放。我希望当在另一个表格单元格中按下另一个按钮时,它会停止播放声音。

这是我的代码

.h 文件

我在@interface 括号 AVAudioPlayer *talkSound; 上输入这个

.m

NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:@"kumusta" ofType:@"m4a"];
if (talkSound.playing) {
[talkSound stop];
[talkSound release];
}
talkSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
talkSound.delegate = self; /*with this here it says theres no delegate so i go an add AVAudioPlayerDelegate and i get tons of errors of connot find protocol declaration*/
[talkSound play];
4

1 回答 1

1

我不知道这是否是问题所在,但您不需要每次都释放并重新分配 AVAudioPlayer。只需停止和启动它就可以正常工作,例如:

if (talkSound.playing) {
    [talkSound stop];
}
[talkSound play];

然后你可以只分配/初始化一次 AVAudioPlayer,也许在 viewDidLoad 中。

于 2009-08-07T20:45:00.413 回答