停止一个声音在播放另一个之前

我知道这是之前在这里问过的,但是我不明白这个答案。 我正在制作一个音板,当您点击其中一个button来播放声音时,我希望已经播放的那个可以停止播放。 我知道我需要添加AudioServicesDisposeSystemSoundID(soundID); ,但我在哪里添加它? 这是我的代码:

 - (IBAction)beyond:(id)sender { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"beyond" ,CFSTR ("wav") , NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); AudioServicesPlaySystemSound(soundID); } - (IBAction)Years:(id)sender { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"years" ,CFSTR ("wav") , NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); AudioServicesPlaySystemSound(soundID); } 

这里的问题是你在你的方法中将soundID定义为一个局部variables,而不是你的类中的一个实例variables。 当您使用实例variables时,您可以稍后从类中的任何方法访问它。

正如所说的那样。 您需要创build一个实例variables(或伊娃),以便多个方法可以引用相同的variables。 正如你已经知道的AudioServicesDisposeSystemSoundID()停止当前正在播放的SystemSoundID 。 一旦你声明了一个伊娃,你不需要在每个方法中声明variables。 所以合乎逻辑的结论是。

 @implementation ViewControllerClassNameHere{ SystemSoundID soundID; } - (IBAction)beyond:(id)sender { AudioServicesDisposeSystemSoundID(soundID); CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"beyond" ,CFSTR ("wav") , NULL); AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); AudioServicesPlaySystemSound(soundID); } - (IBAction)Years:(id)sender { AudioServicesDisposeSystemSoundID(soundID); CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"years" ,CFSTR ("wav") , NULL); AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); AudioServicesPlaySystemSound(soundID); } 

一旦你创build了SystemSoundID你也可以(肯定)要CFRelease() CFURLRef你复制(所以你不泄漏内存)。 事实上,你可能会考虑让你的CFURLRef的ivars,以免不断地复制它们。 你也可能想调用AudioServicesDisposeSystemSoundID(soundID);dealloc

之前回答: 在Xcode中停止声音

但我可以重复我的答案。

总结一下,停止声音文件的语句是:

 AudioServicesDisposeSystemSoundID (soundFileObject); 

解释(重要的理解):

我有一个表视图与18个不同的声音文件。 当用户select一行时,必须发出相应的文件,select另一行时,必须先停止前一个声音,再播放其他声音。

所有这些东西都是必要的:

1)添加到您的项目“AudioToolbox.framework”里面的“构build阶段”,“链接二进制库”

2)在头文件(在我的项目中它调用“GenericTableView.h”)添加这些句子:

 #include <AudioToolbox/AudioToolbox.h> CFURLRef soundFileURLRef; SystemSoundID soundFileObject; @property (readwrite) CFURLRef soundFileURLRef; @property (readonly) SystemSoundID soundFileObject; 

3)在实现文件(在我的项目中调用“GenericTableView.m”)添加这些句子:

 @synthesize soundFileURLRef; @synthesize soundFileObject; 

而在你的“行动”实施或在我的情况下,在:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // // That is the question: a way to STOP sound file // // ----------------------------------------------------------------------- // Very important to STOP the sound file AudioServicesDisposeSystemSoundID (soundFileObject); // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // In my project "rowText" is a field that save the name of the // corresponding sound (depending on row selected) // It can be a string, @"Alarm Clock Bell" in example // Create the URL for the source audio file. // URLForResource:withExtension: method is new in iOS 4.0. NSURL *soundURL = [[NSBundle mainBundle] URLForResource:rowText withExtension:@"caf"]; // Store the URL as a CFURLRef instance self.soundFileURLRef = (CFURLRef) [soundURL retain]; // Create a system sound object representing the sound file. AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject); AudioServicesPlaySystemSound (soundFileObject); [soundURL release]; } 

4)最后,在同一个实现文件(在我的项目中调用“GenericTableView.m”):

 - (void)dealloc { [super dealloc]; AudioServicesDisposeSystemSoundID (soundFileObject); //CFRelease (soundFileURLRef); } 

我必须评论“CFRelease(soundFileURLRef)”,因为应用程序在用户离开表视图时意外结束。

所有这些代码是由苹果公司的比例。 在这个链接,你甚至可以find一个示例代码,直接在你的iPhone模拟器中testing。

https://developer.apple.com/library/ios/samplecode/SysSound/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008018-Intro-DontLinkElementID_2