【问题标题】:Static method variable issue静态方法变量问题
【发布时间】:2012-11-22 11:46:25
【问题描述】:

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

【问题讨论】:

    标签: iphone xcode static-methods


    【解决方案1】:

    您必须将 id 传递给 stopSound(与播放声音时相同),或者如果您的音频播放器一次只播放 1 个声音,则在玩。

    【讨论】:

    【解决方案2】:

    您可以将 soundID 创建为 ivar,例如:

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

    其他方式使用它作为全局变量

    【讨论】:

    • 我在发布到 stackverflow 之前尝试了它作为第一个选项。在类方法中访问的实例变量。
    猜你喜欢
    • 2015-07-22
    • 2012-06-29
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多