You can't stop a sound that's playing via "AudioServicesPlaySystemSound
", but what you can do is use "AVAudioPlayer
" instead.
Keep a reference to your "AVAudioPlayer
" in your object and then you can call "stop
" on it when you need to halt it.
Play
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>
AVAudioPlayer *player;
// ...
NSString *path;
NSError *error;
path = [[NSBundle mainBundle] pathForResource:@"albina" ofType:@"m4a"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
player.volume = 0.5f;
[player prepareToPlay];
[player setNumberOfLoops:0];
[player play];
}
Stop
if (player != nil)
{
if (player.isPlaying == YES)
[player stop];
}