我已经阅读了苹果的文档,关于AVAudioRecorder的使用。链接如下:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW6。我找到了一个录制声音的函数,但是我的代码在此处崩溃:
我搜索了很多次,但没有找到解决方案。我还找到了一些其他的源代码,但它们也有同样的问题(在
这是我的
LyricsController 是一个继承自 UIViewController 的类,遵循 AVAudioSessionDelegate、AVAudioRecorderDelegate 和 MBProgressHUDDelegate 协议。该类定义了一个“代码块”,其中包含了一些属性和方法。
[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;
}
}
My .h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import "MBProgressHUD.h"
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;