我发现了一个记录声音的功能。但是我的代码崩溃了
[soundRecorder prepareToRecord];
我用Google搜索了这么多。但我找不到任何解决方案。我还发现了一些其他的源代码。但是他们也遇到了同样的问题(崩溃在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;
答案 0 :(得分:3)
这只是因为你使用了断点。我遇到了同样的问题,我的代码没有崩溃但它在prepareTorecord
作为断点停止,因为即使我没有在它上面放置任何断点。如果禁用xcode的断点,它也将在模拟器上运行。
答案 1 :(得分:1)
引自this answer:
添加您的异常断点并从&#34; All&#34;编辑异常类型。 to&#34; Objective-C例外&#34; AudioToolbox中的某些类会抛出常规的C ++异常。您可以通过这种方式过滤它们。
至于原因:
&#34; AVAudioPlayer不应该抛出随机异常&#34; - 这不在我们手中。这就是框架编写的方式,你必须忍受它。虽然,我同意这不是一个好主意