Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepareToRecord crashing

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;

2 Answers

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.

like image 160
Ankush Avatar answered Jan 31 '26 01:01

Ankush


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

like image 43
Michael J Petrie Avatar answered Jan 31 '26 02:01

Michael J Petrie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!