2012-02-11 156 views
-4

我知道這是之前在這裏提出的問題,但我不明白答案。我正在製作一個音板,當你點擊其中一個按鈕來播放聲音時,我希望已經播放的那個可以停止播放。我知道我需要添加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); 
    } 
+0

你的方法名是不知道的用戶界面看起來像什麼不可理解的。爲什麼你的IBAction方法被命名爲「beyond」和「Years」? – bneely 2012-02-11 21:18:32

+0

Beyond和Years是播放聲音和按鈕的名稱。 – user1176046 2012-02-11 21:30:44

回答

1

這裏的問題是你定義soundID爲你的方法中的局部變量,而不是因爲你的類內的實例變量。當您使用實例變量時,您可以稍後從類中的任何方法訪問它。

+0

那麼,我該如何解決這個問題? – user1176046 2012-02-11 21:42:57

+1

查看NJones的答案。這是一個相當基本的面向對象編程概念;我鼓勵你查看一些關於面向對象編程的文檔。如果你要用Objective-C做很多工作,我會親自推薦Big Nerd Ranch的Objective-C和iOS編程書籍。 – bneely 2012-02-11 22:30:23

1

正如老實說的那樣。您需要創建一個實例變量(或ivar),以便多個方法可以引用相同的變量。正如你已經知道AudioServicesDisposeSystemSoundID()停止當前播放SystemSoundID。一旦你聲明瞭一個伊娃,你不需要在每種方法中聲明變量。所以合乎邏輯的結論是。

@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); 
} 

你也可以(肯定)要CFRelease()CFURLRef是你複製一旦你創建了SystemSoundID(這樣你就不會泄漏內存)。事實上,你可能會考慮製作你的網站,以免經常複製它們。您也可能想撥打dealloc致電AudioServicesDisposeSystemSoundID(soundID);

0

它面前回答: Stopping sound in Xcode

但我可以重複我的答案。

彙總,停止聲音文件的一句話是:

AudioServicesDisposeSystemSoundID (soundFileObject); 

解釋(重要的是理解):

我有18個不同的聲音文件表視圖。當用戶選擇一行時,必須發出相應的文件,並在選擇另一行時,必須停止前一個聲音並播放其他聲音。

所有這些東西是必要的:

1)添加到您的項目「AudioToolbox.framework」裏面的「構建階段」,裏面的「二進制鏈接與圖書館」

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)」,因爲應用程序在用戶離開表視圖時意外結束。

所有這些代碼是由蘋果比例。在這個鏈接中,你甚至可以找到一個示例代碼,直接在你的iPhone模擬器中測試。

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

相關問題