3

我从苹果文档 http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW6中阅读了有关 AVaudioRecorder 的信息

我找到了一个记录声音的功能。但我的代码正在崩溃

[soundRecorder prepareToRecord]; 

我用谷歌搜索了很多。但我没有找到解决方案。我还发现了一些其他源代码。但它们也有同样的问题(崩溃prepareToRecord)。任何人都可以帮助我吗...

野兔是我的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;
}

}

我的 .h 文件

#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;
4

2 回答 2

3

这只是因为您使用了断点。我遇到了同样的问题,我的代码没有崩溃,但它prepareTorecord作为断点停止,因为即使我没有在其上放置任何断点。如果您禁用 xcode 的断点,它也将在模拟器上运行。

于 2014-01-11T19:38:49.400 回答
1

从这个答案中引用:

添加您的异常断点并将异常类型从“All”编辑为“Objective-C exceptions” AudioToolbox 中的某些类会引发常规 C++ 异常。您可以通过这种方式过滤掉它们。

至于为什么:

“AVAudioPlayer 不应该抛出随机异常”——这不在我们手中。这就是框架的编写方式,你必须忍受它。虽然,我同意这不是一个好主意

于 2014-07-15T21:53:59.267 回答