我正在使用viewDidLoad
方法播放声音文件。声音文件保存在文档目录中。在第一次加载视图时成功播放。但是当我弹出这个视图并返回时,它不会播放。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePathSound =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"sound.caf"]]];
if ([[NSFileManager defaultManager] fileExistsAtPath:localFilePathSound]) {
//NSString *clapPath = [[NSBundle mainBundle] pathForResource:@"scaryVoice" ofType:@"wav"];
CFURLRef clapURL = (CFURLRef ) [NSURL fileURLWithPath:localFilePathSound];
AudioServicesCreateSystemSoundID (clapURL, &clappingFileId);
AudioServicesPlaySystemSound(clappingFileId);
}
// Do any additional setup after loading the view from its nib.
else{
NSString *clapPath = [[NSBundle mainBundle] pathForResource:@"scaryVoice" ofType:@"wav"];
CFURLRef clapURL = (CFURLRef ) [NSURL fileURLWithPath:clapPath];
AudioServicesCreateSystemSoundID (clapURL, &clappingFileId);
AudioServicesPlaySystemSound(clappingFileId);
}
}