3

I have read about AVaudioRecorder from apple documentation http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW6

and i found a function Which records sound. but my code is crashing on

[soundRecorder prepareToRecord]; 

i googled for this so much.But i found no solution. i also found some other source codes.but they also have same problem (crashing on prepareToRecord). can anybody help me please...

hare is my IBAction

- (IBAction) recordOrStop: (id) sender {

if (recording) {

    [soundRecorder stop];
    recording = NO;
    self.soundRecorder = nil;

    [recordOrStopButton setTitle: @"Record" forState:
     UIControlStateNormal];
    [recordOrStopButton setTitle: @"Record" forState:
     UIControlStateHighlighted];
    [[AVAudioSession sharedInstance] setActive: NO error: nil];

} else {

    [[AVAudioSession sharedInstance]
     setCategory: AVAudioSessionCategoryRecord
     error: nil];

    NSDictionary *recordSettings =
    [[NSDictionary alloc] initWithObjectsAndKeys:
     [NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
     [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
     [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
     [NSNumber numberWithInt: AVAudioQualityMax],
     AVEncoderAudioQualityKey,
     nil];
    AVAudioRecorder *newRecorder =
    [[AVAudioRecorder alloc] initWithURL: soundFileURL
                                settings: recordSettings
                                   error: nil];



    self.soundRecorder = newRecorder;

    soundRecorder.delegate = self;
    [soundRecorder prepareToRecord];
    [soundRecorder record];
    [recordOrStopButton setTitle: @"Stop" forState: UIControlStateNormal];
    [recordOrStopButton setTitle: @"Stop" forState: UIControlStateHighlighted];

    recording = YES;
}

}

My .h file

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import "MBProgressHUD.h"

@interface LyricsController : UIViewController<AVAudioSessionDelegate,AVAudioRecorderDelegate, MBProgressHUDDelegate>{

IBOutlet UITextView *LyricsView;
MBProgressHUD *HUD;
AVAudioRecorder * soundRecorder;
NSURL *soundFileURL;  

BOOL recording;
BOOL playing;

__weak IBOutlet UIButton *recordOrStopButton;
}@property(strong,nonatomic)NSURL *soundFileURL;
@property(nonatomic,strong)AVAudioRecorder * soundRecorder;
  • My PrepareToRecord sometime works and sometime not. why is this happning –  May 23 '12 at 12:30
  • [soundRecorder record] is only one line after [soundRecorder prepareToRecord]. Maybe the buffer is not ready. Make a new button labeled "Ready to record" attached to a new IBAction "Record". There put the [soundRecorder record] – Teofilo Israel Vizcaino Rodrig May 23 '12 at 12:58
  • but execution of code has been done only on the [soundRecorder prepareToRecord] by Compiler. Compiler do not even start compiling [soundRecorder record]. I have Put Sleep(2) between [soundRecorder prepareToRecord] and [soundRecorder record] statements to increase time for Buffer To Be ready but again same crash happening. –  May 25 '12 at 08:06
  • 1
    hey guys my app is not crashing on Device it is crashing just on simulator.Then I Have reset simulator and Clean The Xcode (products->Clean). then it again running on simulator perfectly. hope it will not crash again. –  May 25 '12 at 08:25

2 Answers2

3

It is just because you used breakpoints. I had the same problem, my code was not crashing but it was stoping at prepareTorecord as a breakpoint as even I did not put any breakpoint on it. If you disable the breakpoints for xcode it will be running on simulator too.

Ankush
  • 2,405
  • 3
  • 22
  • 45
1

Quoted from this answer:

Add your exception breakpoint and edit the exception type from "All" to "Objective-C exceptions" Some classes in AudioToolbox throw regular C++ exceptions. You can filter them off this way.

As for why:

"AVAudioPlayer shouldn't throw random exceptions" - This is not in our hands. That's the way the framework is written and you have to live with it. Though, i agree that it's not a good idea

Community
  • 1
  • 1
Michael J Petrie
  • 183
  • 4
  • 13