OStack程序员社区-中国程序员成长平台

标题: iphone:AVAudioRecorder 录音总是返回 FALSE [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:02
标题: iphone:AVAudioRecorder 录音总是返回 FALSE

我正在使用 AVAudioRecorder 来录制声音,但是我遇到了问题。

调用[recorder record]方法后,recorder.recording的值一直返回FALSE,让我无法停止录音。

我在 iOS 5 上使用 XCode 4.2,我的 iPad 也是 iOS 5 上的 ipad 2。

这是我的代码:

    self.currentFileName = [GlobalRecording newRecordingFileName];
    NSURL *url = [NSURL fileURLWithPath:[[GlobalRecording recordingDirectory]    stringByAppendingPathComponent:currentFileName]];
NSError *err = nil;

AVAudioRecorder *aRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordingSettings error:&err];
if( err ){
    NSLog(@"could not create a recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}

self.startRecordingTime = [NSDate date];
recordingLength = 0.0f;

//  setup recorder and start recording
[aRecorder setDelegate:self];
aRecorder.meteringEnabled = YES;
[aRecorder record];

self.recorder = aRecorder;
BOOL test = recorder.recording;
[aRecorder release];



Best Answer-推荐答案


尝试在 [audioRecord prepareToRecord] 之后使用 AVAudioSession

    NSError *err = nil;
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

// route to speaker
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(ASRoute), &ASRoute);

if (err)
{
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}

[audioSession setActive:YES error:&err];

关于iphone:AVAudioRecorder 录音总是返回 FALSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7914712/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4