静态方法变量问题

时间:2012-11-22 11:46:25

标签: iphone xcode static-methods

我创建了处理声音的课程。但我有问题阻止声音?我无法将变量从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

2 个答案:

答案 0 :(得分:1)

您必须将id传递给stopSound(与播放声音时相同),否则如果您的音频播放器一次只播放1个声音,则将soundID作为静态参数存储在课堂上播放。

答案 1 :(得分:0)

您可以将声音ID创建为ivar,例如:

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

其他方式将其用作全局变量

相关问题