View Full Version : Microphone code doesn't run on iPhone device

Feb 8, 2012, 07:34 AM
I am trying to run the following code on my device with no success. Although the code works perfectly on Simulator. I have been following this tutorial. It simply crash on device.


Code is:

@interface MicBlowViewController : UIViewController
AVAudioRecorder *recorder;
NSTimer *levelTimer;
double lowPassResults;

- (void)levelTimerCallback:(NSTimer *)timer;


- (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,

NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

if (recorder)
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 3
target: self
selector: @selector(levelTimerCallback:)
userInfo: nil
repeats: YES];
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.95)
NSLog(@"Mic blow detected");

Feb 8, 2012, 09:25 AM
Does your stack trace show anything?

Feb 9, 2012, 12:46 AM
I am using iPhone configuration utility to see NSLog statements. From there, I see the following lines. I am sharing the first 2 lines.

First few lines of log are:

unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.yourcompany.MicBlow[0xe0ef]) Job appears to have crashed: Bus error

unknown SpringBoard[27] <Warning>: Application 'MicBlow' exited abnormally with signal 10: Bus error