我目前正在开发一个支持录音的游戏项目。该程序基于 WiEngine ,一种类似于 cocos2d-x 的游戏引擎。
事情就是这样,我尝试了很多方法,但是每次我调用 [myRecorder Record] 时它总是返回 NO。然后我明确地调用了 PrepareRecord ,它也返回 No 。
这是我如何初始化记录器的代码
- (void)initRecorder
{
if(self.myRecoder!=nil)
{
[self.myRecoder release];
self.myRecoder=nil;
}
NSDictionary *myRecorderParam = [[NSDictionary alloc]initWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0],AVSampleRateKey,
[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithInt:1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMedium],AVEncoderAudioQualityKey,
nil];
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocumentPath = [pathArray objectAtIndex:0];
DocumentPath = [DocumentPath stringByAppendingString:@"/luyin.wav"];
self.myRecoder = [[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString:DocumentPath]
settings:myRecorderParam
error:nil];
[self.myRecoder setDelegate:self];
}
我像这样打电话给记录
- (void)beginRecord
{
NSLog(@"begin record");
// AVAudioSession *audioSession = [AVAudioSession sharedInstance];
// NSError *err = nil;
// [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
// if(err){
// NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
// return;
// }
// err = nil;
// [audioSession setActive:YES error:&err];
// if(err){
// NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
// return;
// }
if(self.myRecoder==nil)
{
NSLog(@"recoder null");
}
BOOL resultPre = [[self myRecoder] prepareToRecord];
if(resultPre)
{
NSLog(@" record pre yes ");
}
else
{
NSLog(@" record pre no ");
}
BOOL result = [[self myRecoder] record];
if(result)
{
NSLog(@" record yes ");
}
else
{
NSLog(@" record no ");
}
}
根据 NSLogs ,我确定初始化似乎没有失败。
我也尝试初始化 AudioSession ,但它也不起作用
请帮忙