I put a button on a table view cell that play sound when they are pressed but
-
12-09-2019 - |
Question
I put a button on a table view cell that play sound when they are pressed, when I press the button it stop the sound. But pressing another button on another cell it doesn't plays another sound while the other still playing. I hope that when another button is pressed in another table cell it will stop the playing sound.
Here is my code
.h file
I enter this on @interface bracket 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];
Solution
I don't know if this is the problem, but you don't need to release and reallocate the AVAudioPlayer every time. Just stopping and starting it should work fine, like:
if (talkSound.playing) {
[talkSound stop];
}
[talkSound play];
Then you can just alloc/init the AVAudioPlayer once, perhaps in viewDidLoad.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow