I have a very puzzling problem, which I really cannot work out what is going wrong.
Basically I am making an application that when a sound is made by someone will cause my application to display a random image and play a sound at the same time.
The puzzerling thing is this it works brilliantly in the simulator but does when I put it on my device the image will randomize but no sound will play. I have looked through the forums and tried to eliminate as many causes to the problem the sound file is a cif and I have made sure that my device volume is indeed on.
So the problem is nothing to do with the sound file format.
I have even tried testing out using code so that when I shake the device it will initiate the random image and the sound, this works perfectly on my device.
I think whatever is causing my sound not to play on the device has something to do with the code used to detect sound. But I cannot work out what exactly I have done wrong.
The code is below I hope someone can help
Code:
#import "ScaryEyesViewController.h"
@implementation ScaryEyesViewController
-(IBAction) img1 {
UIImage *img = [UIImage imageNamed:@"Image2.png"];
[imageView setImage:img];
}
// -(IBAction) img2 {
// int randomInt = arc4random()%3;
// NSString *fileName = [NSString stringWithFormat:@"Image%d.png", randomInt];
// UIImage *img = [UIImage imageNamed:fileName];
// UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.05 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else
NSLog([error description]);
}
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.65)
NSLog: [self micHadNoise];
//// micHadNoise is essential
}
- (void) micHadNoise {
int randomInt = arc4random()%3;
NSString *fileName = [NSString stringWithFormat:@"Image%d.png", randomInt];
UIImage *randomImage = [UIImage imageNamed:fileName];
imageView.image = randomImage;
// NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"sound1" ofType: @"mp3"];
// NSURL *url = [NSURL fileURLWithPath:urlAddress];
// NSError *error;
// AVAudioPlayer *audioPlayer = [[AVAudio alloc] initWithContentsOfURL:url error&error];
// audioPlayer.numberOfLoops = 0;
// if (audioPlayer == nil)
// {
// NSLog ([error description]);
// }
// else {
// {
// [audioPlayer play];
// }
// //
//
//NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
// AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
// AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
// theAudio.numberOfLoops = 1;
// if (theAudio == nil)
NSString *path = [[NSBundle mainBundle] pathForResource:@"farSound" ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
// int randomNumber = arc4random() % 6;
// NSString *tmpFileName = [[NSString alloc] initWithFormat:@"sound%d", randomNumber];
// NSString *fileName2 = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"mp3"];
// SystemSoundID soundID;
// AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName2], &soundID);
// AudioServicesPlaySystemSound (soundID);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end