我一直在网上搜索一种在 Objective C 中播放声音的方法,我找到了很多答案。但是,当我尝试播放某些东西时,会弹出三个“Apple Mach-O Linker Errors”。
这是我的代码:
视图控制器.m
-(IBAction)shoot{
tempSound *sound = [[tempSound alloc]init];
[sound playSound:@"test" :@"wav"]
}
临时声音.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
临时声音.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
提前致谢!