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

次に、おそらくviewDidLoadで、AVAudioPlayerを1回だけ割り当て/初期化できます。

于 2009-08-07T20:45:00.413 に答える