我一直在网上寻找在Objective C中播放声音的方式,并且我找到了很多答案。但是,当我尝试播放三个“Apple Mach-O链接器错误”时弹出。如何使用AudioServices播放声音效果 - 目标C
这里是我的代码:
viewController.m
-(IBAction)shoot{
tempSound *sound = [[tempSound alloc]init];
[sound playSound:@"test" :@"wav"]
}
tempSound.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface tempSound : NSObject{
SystemSoundID audioEffect;
}
-(void)playSound: (NSString*) fName : (NSString*) ext;
@end
tempSound.m
@implementation tempSound
-(void)playSound:(NSString *)fName :(NSString *)ext
{
NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL *pathURL = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else{
NSLog(@"Error, file not found: %@",path);
}
}
@end
在此先感谢!
你应该用大写来命名你的类。 –
不是说它会解决你的问题,请查看h2co3的答案,但它会帮助你组织起来。 –
好的,谢谢。我会记住它。 – Bogidon