I am playing a sound file in viewDidLoad
method. The sound file is saved in document directory. It is successfully played when the view loaded first time. But when I pop this view and come back then it is not played.
here is my code:
- (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);
}
}
If you want to play this method after coming back from a view.
Just write the above code in the - (void)viewWillAppear:(BOOL)animated
or - (void)viewDidAppear:(BOOL)animated
method.
- (void)viewWillAppear:(BOOL)animated
{
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);
}
}