2012-11-22 87 views
0

我創建了處理聲音的類。但我有阻止聲音的問題?我無法將變量從playSound發送到stopSound AudioServicesDisposeSystemSoundID(soundID)。 如何處理?我真的不想實例化它或我必須?靜態方法變量問題

@implementation SoundPlayer 
+(void)playSound:(NSString *)filename ofType:(NSString *)extension { 
    SystemSoundID soundID; 
    NSString *soundFile = [[NSBundle mainBundle] pathForResource:filename ofType:extension]; 
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID); 
    AudioServicesPlaySystemSound(soundID); // error - soundID is undeclared 
} 

+(void)stopSound 
{ 
    AudioServicesDisposeSystemSoundID(soundID); 
} 

@end 

回答

1

你要麼必須ID傳遞給stopSound(一樣,當你播放聲音),否則如果你的音頻播放器一次只能播放1種聲音,存儲soundID作爲一個靜態PARAM當你玩的時候上課。

+0

,我怎麼能聲明靜態PARAM? – alexhajdu

+0

'@interface TheClass:NSObject +(int)count; @ end'參見:http://stackoverflow.com/questions/6035824/objective-c-static-class-variables – cleverbit

0

您可以創建soundID作爲伊娃,是這樣的:

@interface SoundPlayer(){ 
    SystemSoundID soundID; 
} 
@end 

其它方式使用它作爲全局變量

+0

我試過它作爲第一個選項,然後發佈到stackverflow。在類方法中訪問的實例變量。 – alexhajdu