开发者

xCode - How to stop one action when another occurs

开发者 https://www.devze.com 2023-02-18 17:54 出处:网络
I have an app that开发者_高级运维 speaks words. However, when word B is pressed, I want to stop word A. Is there a way to do that? This is my code for speaking the words:

I have an app that开发者_高级运维 speaks words. However, when word B is pressed, I want to stop word A. Is there a way to do that? This is my code for speaking the words:

-(IBAction)sayIt:(id)sender
{
//NSLog(@"%s", __FUNCTION__);
CFBundleRef mainBundle = CFBundleGetMainBundle();
FlashCardsAppDelegate *mainDelegate = (FlashCardsAppDelegate *)[[UIApplication sharedApplication] delegate];
CFURLRef soundFileURLRef;
soundFileURLRef= CFBundleCopyResourceURL (mainBundle, (CFStringRef)[mainDelegate soundFile], CFSTR("caf"), NULL);   
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}   


you can use AVAudioPlayer as a class member, call player.stop and player.play to stop and start audio. if you need to change the sound after stopping the player use InitWithData


Looking at the docs, it does not look like you can stop it playing the alert until its complete.

But you can use a callback to tell when it has finished playing to avoid playing two sounds. Look at:

AudioServicesAddSystemSoundCompletion

Discussion Because a system sound may play for several seconds, you might want to know when it has finished playing. For example, you may want to wait until a system sound has finished playing before you play another sound.

and

AudioServicesSystemSoundCompletionProc

Discussion Because a system sound may play for up to 30 seconds, the AudioServicesPlaySystemSound function executes asynchronously (that is, it returns immediately). This callback gets invoked when a specified system sound has finished playing. You can use this callback, for example, to help you avoid playing a second sound while a first sound is still playing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号