从C代码转换为ObjC

时间:2012-02-19 22:46:00

标签: objective-c c object

我是C的经验丰富的程序员,但不是客观的c。我试图通过在目标c中给他这段代码来帮助我的朋友。顺便说一下,这适用于iPhone应用程序。我有必要的所有基础,你可以看到,使用 - (IBAction)方法声明我的声音是声音A ...此外,声音是一个不大于100 kb的简单.wav文件。

- (IBAction)soundA:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, 
        (CFStringRef) @"soundA", CFSTR ("wav"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

非常感谢任何帮助!非常感谢。

  • 郑国

1 个答案:

答案 0 :(得分:1)

也许尝试将其从无符号整数更改为SystemSoundID

 - (IBAction)soundA:(id)sender {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, 
            (CFStringRef) @"soundA", CFSTR ("wav"), NULL);

        SystemSoundID soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }

或采用更客观的方法:

 - (IBAction)soundA:(id)sender {
        NSString *path = [NSString stringWithFormat: @"%@/%@",
                     [[NSBundle mainBundle] resourcePath], name];


        NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
        SystemSoundID soundID;
        AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }